3

我有以下函数需要在另一个函数中调用。我不知道该怎么做?

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
    }
}
4

3 回答 3

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();
于 2013-03-28T09:08:36.140 回答
1

IsValidUser返回一个int显然没有result属性的。但我假设您只想使用该值:

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

但是您应该首先考虑返回 a bool,因为这样更具可读性:

private bool IsValidUser()
{            
    bool result = false;
    // ...
    return result;
}
于 2013-03-28T09:11:15.820 回答
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

    }
}
于 2013-03-28T09:09:04.497 回答