简体   繁体   中英

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

I wrote following code...but i am getting 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 as given 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;

    }
}

What if value == 3 ?

You could rewrite the code like this:

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
}

Because if the value variable were equal to 3 your method wouldn't return anything.

And just for kicks, here's a short rewrite of your code that I think would work splendid.

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;
}

You only query the database 1 time and you're able to get everything you need in order to see if it's a valid login attempt.

You get the error because it is possible for the function to end (that is, traverse a code path ) without returning a value. To fix the error, add an else clause to the end of your conditional:

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

You may know that the result of your ExecuteScalar call is 0 or 1, but the compiler cannot know that in advance. Make your "else if" a standard else or provide another return value before the end of the method.

因为有可能value不是1或2,并且你的外部if那个分支没有return语句。

You don't have return at the end of your method. If method is not void and returning some value than compiler checks that it always returns a value. Your method may not return value in several cases.

It's recommended that always return your result with ONLY ONE way 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM