我需要在另一个函数中调用以下函数。我不知道怎么做?
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
}
}
答案 0 :(得分:3)
您的函数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();
答案 1 :(得分:1)
IsValidUser
会返回int
,但显然没有result
属性。但我假设您只想使用该值:
int valUserResult = IsValidUser();
bool blnValidUser = valUserResult == 1;
但你应该考虑首先返回一个bool
,因为那样会更具可读性:
private bool IsValidUser()
{
bool result = false;
// ...
return result;
}
答案 2 :(得分:0)
修改功能如下
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
}
}