2

I am getting an error, I cannot return a value from the function below. Help will be appreciated.

private void UserExiest(string username)
{
    SqlConnection myConnection = new SqlConnection("user id=test;" +
            "password=test;" +
            "server=.;" +
            "Trusted_Connection=yes;" +
            "database=DB; " +
            "MultipleActiveResultSets=True;" +
            "connection timeout=30");

    myConnection.Open();
    SqlCommand CHECKNPC = new SqlCommand("select struserid from USERDATA where strUserId = '" + username + "'", myConnection);
    SqlDataReader NpcReader = CHECKNPC.ExecuteReader();
    if (NpcReader.HasRows)
    {
        return "1";
    }
    else
    {
        return "0";
    }
    myConnection.Close();
}
4
  • Change to private int UserExiest Commented Apr 26, 2013 at 12:10
  • 2
    you never close your connection, you shouldnt always rely on GC EDIT and just return NPCReader.HasRows Commented Apr 26, 2013 at 12:11
  • Use the Using statement for the SQLConnection, way better, and will dispose after finishing Commented Apr 26, 2013 at 12:15
  • 2
    The word is spelled exist instead of exiest so i would also change the method name. It would hurt my eyes otherwise all the time if I were you. Commented Apr 26, 2013 at 12:25

6 Answers 6

4

Your function has return type void. You can't return a string from there. Change your function signature to :

private string UserExist(string username)

Its better if you return bool, since you are checking if NpcReader.HasRows and then returning "1" in case of true and "0" in case of false. Also its better if you close the connection, before returning the value.

Always use SqlParameter or Parameterized query , your current query is open for SQL Injection.

Sign up to request clarification or add additional context in comments.

Comments

3

You method signature doesn't have a return type:

private void UserExist(string username)

You probably want to return a boolean

private bool UserExist(string username)

return true;
// or
return false;

Comments

3
private bool UserExist(string username)
{
    using (var con = new SqlConnection("..."))
    {
        con.Open();
        using (var cmd = new SqlCommand("...", con))
        {
            using (var r = cmd.ExecuteReader())
            {
                return r.HasRows;
            }
        }
    }
}

Despite the fact your orginal function doesn't return any value and doesn't close the connection - you're trying to implement unnecessary logic (returning string flags instead of returning ready to use bool value, which is the result of reader.HasRows expression). Finally remember about using statements while working with types implementing IDisposable, which guarantees to perform application-defined tasks associated with freeing, releasing, or resetting unmanaged resources (see msdn).

Comments

1
private void UserExiest(string username)

Change this to:

private string UserExiest(string username)

seeing as you appear to be returning a string...

Comments

0

Are you using return statements in your method, while its definition show you must return void? Change your definition methode to :

private string UserExiest(string username)

Comments

0

As suggested in many response here, you need to add a return type to your function. Change the void to string to return the value. As the value is a 1 or 0 it might be better to return true or false.

Also, you have your connection close statement after the return. This statement would not be triggered in normal flow of the application. Try the following :

private bool UserExiest(string username)
{
    SqlConnection myConnection = new SqlConnection("user id=test;" +
            "password=test;" +
            "server=.;" +
            "Trusted_Connection=yes;" +
            "database=DB; " +
            "MultipleActiveResultSets=True;" +
            "connection timeout=30");
    try
    {
        myConnection.Open();
        SqlCommand CHECKNPC = new SqlCommand("select struserid from USERDATA where strUserId = '" + username + "'", myConnection);
        SqlDataReader NpcReader = CHECKNPC.ExecuteReader();
        return NpcReader.HasRows;
    }
    finally
    {
        if (myConnection.State != System.Data.ConnectionState.Closed)
        {
            myConnection.Close();
        }

    }
}

I have simply added a finally block to ensure the connection is closed and changed the return types to boolean true and false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.