在另一个函数中调用返回布尔值的函数
本文关键字:函数 布尔值 返回 另一个 调用 | 更新日期: 2023-09-27 18:06:29
我需要在另一个函数中调用下面的函数。我不知道怎么做?
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!";
}
}
我试图调用它如下按钮点击事件。
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
}
}
你的函数IsValidUser
被设计为返回一个int
(不管出于什么原因,我不知道,因为它不返回任何东西。这段代码永远不会编译。)
你可以修复它,通过让它像这样返回一个bool
:
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;
}
然后你可以像这样调用你的函数:
blnValidUser = IsValidUser();
IsValidUser
返回一个显然没有result
属性的int
。但我假设你只是想使用这个值:
int valUserResult = IsValidUser();
bool blnValidUser = valUserResult == 1;
但是你应该考虑首先返回一个bool
,因为这样会更容易读:
private bool IsValidUser()
{
bool result = false;
// ...
return result;
}
将函数修改如下
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;
}
}
并按如下方式调用
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
}
}