Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling a function returning boolean in another function

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
    }
}
like image 418
who-aditya-nawandar Avatar asked Mar 24 '26 23:03

who-aditya-nawandar


2 Answers

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();
like image 101
Timothy Groote Avatar answered Mar 26 '26 14:03

Timothy Groote


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;
}
like image 36
Tim Schmelter Avatar answered Mar 26 '26 13:03

Tim Schmelter



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!