3

I have the following function that I need to call in another function. I dont know how to do it?

private int IsValidUser()
{            
    int result = 0;
    string strQuery = "Select Email From AUser Where Email = @Email And Password = @Password ";
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

    SqlCommand Cmd = new SqlCommand(strQuery, con);
    //Cmd.CommandType = CommandType.StoredProcedure;

    Cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
    Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
    con.Open();

    result = (int)Cmd.ExecuteScalar();

    if (result > 0)
    {
        //Session["SessionEmail"] = txtEmail.Text;
        Session[General.S_USEREMAIL] = txtEmail.Text;
        Response.Redirect("~/frmMyAccountMyProfile.aspx");
    }
    else
    {
        Literal1.Text = "Invalid Email/Password!";
    }
}

I am trying to call it as below on button click event.

protected void btnSignIn_Click(object sender, EventArgs e)
{
    // Authenticate User
    bool blnValidUser = false;
    IsValidUser();
    blnValidUser = Convert.ToBoolean(IsValidUser().result.Value);
    if (blnValidUser)
    {
        // on Success - If remember me > Save to cookies
        //SaveUserDetailsToCookie();                       
    }
    else
    {
        // on Failure - Show error msg
    }
}
2
  • 1
    IsValidUser() doesn't return value Commented Mar 28, 2013 at 9:08
  • 1
    IsValidUser returns an int which clearly has no result property. Commented Mar 28, 2013 at 9:09

3 Answers 3

3

Your function IsValidUser is designed to return an int

(for whatever reason is unknown to me, because it returns nothing. this code will never compile.)

you can fix it, by having it return a bool like this:

private bool IsValidUser()
{
        int result = 0;
        //since executeScalar is intended to retreive only a single value
        //from a query, we select the number of results instead of the email address
        //of each matching result.
        string strQuery = "Select COUNT(Email) From AUser Where Email = @Email And Password = @Password ";
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

        SqlCommand Cmd = new SqlCommand(strQuery, con);
        //Cmd.CommandType = CommandType.StoredProcedure;

        Cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
        Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
        con.Open();

        result = (int)Cmd.ExecuteScalar();

        //returning a boolean comparator works like this :
        //will return true if the result is greater than zero, but false if it is not.
        return result > 0;
}

and then you can call your function like this:

blnValidUser = IsValidUser();
Sign up to request clarification or add additional context in comments.

2 Comments

(what i'm leaving out of the example, is that it's better not to do DAL related calls in methods like this, and that you still need to close your connection) but that was not part of the question.
Not only can it work, it's designed to, but rather designed to retreive a single value (see dev.mysql.com/doc/refman/5.0/es/… ) I didn't check your mysql statement, but now you mention it, it makes no sense. i'll update the answer for you
1

IsValidUser returns an int which clearly has no result property. But i assume that you just want to use that value:

int valUserResult = IsValidUser();
bool blnValidUser = valUserResult == 1;

But you should consider to return a bool in the first place because that would be more readable:

private bool IsValidUser()
{            
    bool result = false;
    // ...
    return result;
}

Comments

0

Modify the function as below

private int IsValidUser()
   {
    int result = 0;
    string strQuery = "Select Email From AUser Where Email = @Email And Password = @Password ";
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

    SqlCommand Cmd = new SqlCommand(strQuery, con);
    //Cmd.CommandType = CommandType.StoredProcedure;

    Cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
    Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
    con.Open();

    result = (int)Cmd.ExecuteScalar();

    if (result > 0)
    {
        //Session["SessionEmail"] = txtEmail.Text;
        Session[General.S_USEREMAIL] = txtEmail.Text;
        Response.Redirect("~/frmMyAccountMyProfile.aspx");
    }

    else
    {
        Literal1.Text = "Invalid Email/Password!";
    }
     return result;

}
}

And call it as follows

protected void btnSignIn_Click(object sender, EventArgs e)
 {
    // Authenticate User
    int blnValidUser = IsValidUser();
    //Check if blnValidUser is greater than 0.
    //IsValidUser() will return value greater than 0 if the sql is successful else will return 0
    if (blnValidUser > 0)
    {
        // on Success - If remember me > Save to cookies
        //SaveUserDetailsToCookie();                       
    }
    else
    {
        // on Failure - Show error msg

    }
}

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.