【问题标题】:calling a function returning boolean in another function在另一个函数中调用返回布尔值的函数
【发布时间】:2013-03-18 15:40:18
【问题描述】:

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

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() 不返回值
  • IsValidUser 返回一个 int,它显然没有 result 属性。

标签: c# asp.net boolean


【解决方案1】:

您的函数 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();

【讨论】:

  • (我在示例中省略的是,最好在这样的方法中进行与 DAL 相关的调用,并且您仍然需要关闭连接) 但这不是问题的一部分。
  • 它不仅可以工作,它设计用于,而是设计用于检索单个值(参见dev.mysql.com/doc/refman/5.0/es/…)我没有检查你的mysql语句,但现在你提到它,它没有任何意义。我会为你更新答案
【解决方案2】:

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

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

但您应该首先考虑返回bool,因为这样更易读:

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

【讨论】:

    【解决方案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!";
        }
         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
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 2021-01-06
      • 2012-09-06
      相关资源
      最近更新 更多