2013-03-28 154 views
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 
    } 
} 
+1

IsValidUser()不返回值 – realnero 2013-03-28 09:08:19

+1

'IsValidUser'返回一個'int',它顯然沒有'result'屬性。 – 2013-03-28 09:09:27

回答

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(); 
+0

(我離開這個例子的原因是,最好*不要*在這樣的方法中做DAL相關的調用,並且你仍然需要關閉你的連接),但這不是問題的一部分。 – 2013-03-28 09:16:03

+0

ExecuteScalar如何與Select語句一起使用? – adityawho 2013-03-28 10:16:48

+0

它不僅可以工作,它的*設計*,而是旨在檢索單個值(請參閱http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html ) 我沒有檢查你的mysql語句,但現在你提到它,這是沒有意義的。我會爲你更新答案 – 2013-03-28 10:19:19

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 

    } 
} 
1

IsValidUser返回int這顯然沒有result財產。但我以爲你只是想使用該值:

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

但是,你應該考慮在首位返回bool,因爲這將是更具可讀性:

private bool IsValidUser() 
{    
    bool result = false; 
    // ... 
    return result; 
}