Reputation: 1344
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
}
}
Upvotes: 3
Views: 24455
Reputation: 8643
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();
Upvotes: 3
Reputation: 460068
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;
}
Upvotes: 1
Reputation: 56
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
}
}
Upvotes: 0