Why does this code block say "not all code paths return a value"?

I wrote the following code ... but I get Error like:

Error 1 'LoginDLL.Class1.Login (string, string, string)': not all code paths return a value

Please help me...

Thanks in advance...

My code is below ...

public int Login(string connectionString,string username,string password)
{
    SqlConnection con=new SqlConnection(connectionString);
    con.Open();

    SqlCommand validUser = new SqlCommand("SELECT count(*) from USER where username=@username", con);
    validUser.Parameters.AddWithValue("@username", username);
    int value=Convert.ToInt32(validUser.ExecuteScalar().ToString());
    if (value == 1)
    {
        //check for password
        SqlCommand validPassword = new SqlCommand("SELECT password from USER where username=@username", con);
        validPassword.Parameters.AddWithValue("@username", username);
        string pass = validPassword.ExecuteScalar().ToString();
        if (pass == password)
        {
            //valid login
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else if (value == 0)
    {
        return 2;

    }
}
+3
source share
7 answers

You get an error because it is possible for the function to terminate (i.e. cross the code path) without returning a value. To fix the error, add a sentence elseat the end of your conditional expression:

    if (value == 1)
    {
      // ...
    }
    else if (value == 0)
    {
      // ...
    }
    else {
      // Return a value here.
    }
+2
source

What if value == 3?

You can rewrite the code as follows:

public LoginResult Login(string connectionString, string username, string password)
{
    if (string.IsNullOrEmpty(username)
    {
        return LoginResult.InvalidUser;
    }
    else if (string.IsNullOrEmpty(password)
    {
        return LoginResult.InvalidPassword;
    }

    using (var connection = new SqlConnection(connectionString))
    {
        connection.Open();

        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT password from USER where username=@username";
            command.Parameters.AddWithValue("@username", username);
            var actualPassword = (string)command.ExecuteScalar();

            if (actualPassword == null)
            {
                return LoginResult.InvalidUser;
            }
            else if (password != actualPassword)
            {
                return LoginResult.InvalidPassword;
            }
            else
            {
                return LoginResult.Success;
            }
        }
    }
}

public enum LoginResult
{
    Success,
    InvalidPassword,
    InvalidUser
}
+12

value 3, .

, , , , .

public int Login(string connectionString,string username,string password)
{
  using(var con = new SqlConnection(connectionString)) {
    con.Open();
    var cmdText = "SELECT password from USER where username=@username";
    using (var cmd = new SqlCommand(cmdText, con)) {

      cmd.Parameters.AddWithValue("@username", username);
      object passwordFromDb = userCmd.ExecuteScalar();
      if (passwordFromDb != null) {
          if (password == passwordFromDb.ToString()) {
            return 1;
          }
      }
    }
  }
  return 0;
}

1 , , , , .

+6

, ExecuteScalar 0 1, . "else if" .

+1

, value 1 2, return if.

0

You do not have returnat the end of your method. If the method is not voidand returns some value than the compiler, it always returns the value. Your method may not return a value in several cases.

0
source

He recommended always returning the result using ONLY ONE method in your method.

public int Login(string connectionString,string username,string password)
{
    int result = 0; //Default result value.

    SqlConnection con=new SqlConnection(connectionString);
    con.Open();

    SqlCommand validUser = new SqlCommand("SELECT count(*) from USER where username=@username", con);
    validUser.Parameters.AddWithValue("@username", username);
    int value=Convert.ToInt32(validUser.ExecuteScalar().ToString());
    if (value == 1)
    {
        //check for password
        SqlCommand validPassword = new SqlCommand("SELECT password from USER where username=@username", con);
        validPassword.Parameters.AddWithValue("@username", username);
        string pass = validPassword.ExecuteScalar().ToString();
        if (pass == password)
        {
            //valid login
            result = 1;
        }
        //It is not necessary in this case
        //else
        //{
        //    result = 0;
        //}
    }
    else if (value == 0)
    {
        result = 2;

    }

    return result;
}
0
source

Source: https://habr.com/ru/post/1737844/


All Articles