Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a value from a function - error

Tags:

c#

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();
}
like image 930
Dan272 Avatar asked Apr 23 '26 23:04

Dan272


1 Answers

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.

like image 148
Habib Avatar answered Apr 26 '26 13:04

Habib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!