-4

I'm trying to check if a username with the given passwort exists in my database, if it exists it should return true otherwise it should return false.

My current function looks like the following:

public bool user_check(string username, string password)
{
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'";

    if (this.OpenConnection() == true)
    {
        MySqlCommand cmd = new MySqlCommand(query, connection);
        MySqlDataReader dataReader = cmd.ExecuteReader();
        if (dataReader.HasRows)
        {
            while (dataReader.Read())
            {
                return true;
            }
        }
        else
        {
            return false;
        }
        dataReader.Close();
        this.CloseConnection();
    }
}

But I receive the following error message:

Error CS0161 'database_connector.user_check(string, string)': not all code paths return a value

What am I doing wrong?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
daniel.
  • 128
  • 1
  • 1
  • 10

6 Answers6

3

Just add a return false; at the end. Also you don't need the else return false in the inner if

public bool user_check(string username, string password)
{
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'";

    if (this.OpenConnection())
    {
        MySqlCommand cmd = new MySqlCommand(query, connection);
        MySqlDataReader dataReader = cmd.ExecuteReader();
        if (dataReader.HasRows)
        {
            while (dataReader.Read())
            {
                return true;
            }
        }
        dataReader.Close();
        this.CloseConnection();
    }

    return false;
}

Also see that if you have records then you return true and never close your connection and object. Maybe do this instead:

public bool user_check(string username, string password)
{
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'";

    bool hasRecords = false;

    if (this.OpenConnection())
    {
        MySqlCommand cmd = new MySqlCommand(query, connection);
        MySqlDataReader dataReader = cmd.ExecuteReader();
        if (dataReader.HasRows)
        {
            while (dataReader.Read())
            {
                hasRecords = true;
                break;
            }
        }
        dataReader.Close();
        this.CloseConnection();
    }
    return hasRecords;
}

last thing: look into Parameterized Queires to avoid SQL Injections

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
2

Just add a return false at the end of the method:

public bool user_check(string username, string password)
{
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'";

    if (this.OpenConnection() == true)
    {
        MySqlCommand cmd = new MySqlCommand(query, connection);
        MySqlDataReader dataReader = cmd.ExecuteReader();
        if (dataReader.HasRows)
        {
            while (dataReader.Read())
            {
                return true;
            }
        }
        else
        {
            return false;
        }
        dataReader.Close();
        this.CloseConnection();
    }
    return false;  //<<---- This is where it does not know what to do if any above conditions fail.
}
Sadique
  • 22,572
  • 7
  • 65
  • 91
1

if this check returns false:

 if (this.OpenConnection() == true)

You exit without returning anything.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
1

Error CS0161 occurs when a function that specifies a return type in its signature contains a path through the function that does not return a value. In your case your function does not return a value when the this.OpenConnection() method returns false.

To prevent this error from being reported by the compiler, all paths should return a value:

public bool user_check(string username, string password)
{
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'";

    if (this.OpenConnection() == true)
    {
        MySqlCommand cmd = new MySqlCommand(query, connection);
        MySqlDataReader dataReader = cmd.ExecuteReader();
        if (dataReader.HasRows)
        {
            while (dataReader.Read())
            {
                return true;
            }
        }
        else
        {
            return false;
        }
        dataReader.Close();
        this.CloseConnection();
    }
    return false;
}

I take this opportunity to let you know about SQL injection

Your code is vulnerable to SQL injection because you're concatenating user input into your query. You're encouraged to use parameterized queries, for more information about this topic check out this link

Community
  • 1
  • 1
Wazner
  • 2,962
  • 1
  • 18
  • 24
  • 1
    your code will still get the exception.. you forgot the case where it won't enter the `OpenConnection == true` if – Gilad Green Aug 02 '16 at 07:45
  • @GiladGreen It was my intention to place the return false there as I typed in the body of my answer, but forgot to actually add it to the code. Thank you for the edit :) – Wazner Aug 02 '16 at 07:48
0

That's because you are writing your return inside if while blocks only.Besides I suggest you use parameterized query

public bool user_check(string username, string password){
    string query = "SELECT username, password From swear_tool Where "+
                   "username=@uname and password=@password";
    if (this.OpenConnection() == true){
        using(MySqlCommand cmd = new MySqlCommand(query, connection)){
            cmd.Parameters.AddWithValue("@uname",usename);
            cmd.Parameters.AddWithValue("@password",password);
            using(MySqlDataReader dataReader = cmd.ExecuteReader()){
                if (dataReader.HasRows){
                    while(dataReader.Read()){
                        return true;
                    }
                }
            }
        }
        this.CloseConnection();                    
    }
    return false;
}

Nice and Clean and Safe and Shorter

jonju
  • 2,711
  • 1
  • 13
  • 19
0

The previous answer do not close the connection when a user exists. Try this:

    public bool user_check(string username, string password)
    {
        string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'";

        if (this.OpenConnection())
        {
            try
            {
                using (MySqlCommand cmd = new MySqlCommand(query, connection))
                {
                    using (MySqlDataReader dataReader = cmd.ExecuteReader())
                    {
                        if (dataReader.HasRows)
                        {
                            while (dataReader.Read())
                            {
                                return true;
                            }
                        }
                    }
                }

            }
            finally
            {
                this.CloseConnection();
            }
        }
        return false;
    }
Udo
  • 449
  • 3
  • 13