【问题标题】:Code does not return if the condition is true如果条件为真,代码不会返回
【发布时间】:2012-04-02 02:19:50
【问题描述】:

我需要验证用户提供的信息是否在数据库中,我尝试输入正确的条件,但它不工作它返回一个在数据库中找不到的错误。你能检查我的代码并告诉我发生了什么吗? ,我尝试调试它,但 foreach 循环继续循环并且没有转到 if (isexist) 语句

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        token = FormsAuthentication.HashPasswordForStoringInConfigFile(txtUsername.Text.ToString() + txtAcctNo.Text.ToString(), "MD5");
        try
        {
            bool isExist = false;
            DataSet ds = new DataSet();
            ds = startService.getAllUsersWithoutFilter();
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dRow in ds.Tables[0].Rows)
                {

                    string userName = dRow["UserName"].ToString();
                    string acctNo = dRow["AccountNumber"].ToString();
                    string question = dRow["SecretQuestion"].ToString();
                    string answer = dRow["SecretAnswer"].ToString();

                    if (userName == txtUsername.Text.ToString() && acctNo == txtAcctNo.Text.ToString() && question == cboQuestion.Text.ToString() &&  answer == txtAnswer.Text.ToString())
                    {
                        isExist = true;
                    }
                    else
                    {
                        isExist = false;
                    }

                }

                if (isExist)
                {
                    startService.sendTokenizer(txtUsername.Text.ToString(), token);
                    //update database to change password to standard password
                    startService.inserUserActivity(txtUsername.Text.ToString(), txtAcctNo.Text.ToString(), "Password Reset Request", HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
                    startService.requestReset(txtUsername.Text.ToString(), txtAcctNo.Text.ToString(), token);

                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Text = "<br>We have sent an email to you for the instructions to reset your password. Please check your email.";
                }
                else
                {
                    this.lblMessage.ForeColor = System.Drawing.Color.Red;
                    this.lblMessage.Text = "<br><br>Error - Information cannot be found. Please check and try again. Make sure all the fields are correct.";
                }

            }
        }
        catch
        {
            lblError.Text = "There was an error occured while processing your request. Please try again later.";
        }

    }

【问题讨论】:

    标签: c# asp.net sql-server database web-services


    【解决方案1】:

    我认为当你将 isExist 设置为 true 时,你需要的只是打破你的 foreach 循环。

    if (userName == txtUsername.Text.ToString() && acctNo == txtAcctNo.Text.ToString() && question == cboQuestion.Text.ToString() &&  answer == txtAnswer.Text.ToString())
    {
        isExist = true;
        break; //Found it, so stop looking.
    }
    

    【讨论】:

      【解决方案2】:

      我认为 Joel 对您的问题的直接回答是正确的。

      我要补充一点,您应该重新考虑加载整个用户表并在 Web 服务器上对其进行迭代。为什么不尝试从数据库中选择匹配的行呢?如果您得到匹配,则凭证是有效的。如果不是,则它们无效。

      【讨论】:

      • +1 - 绝对是一个好主意,不要加载整个表,而是利用数据库的力量找到匹配的行进行验证。
      【解决方案3】:

      @Dhenn:您需要在代码中进行以下更改

      protected void btnSubmit_Click(object sender, EventArgs e)
      {
          token = FormsAuthentication.HashPasswordForStoringInConfigFile(txtUsername.Text.ToString() + txtAcctNo.Text.ToString(), "MD5");
          try
          {
              bool isExist = false;
              DataSet ds = new DataSet();
              ds = startService.getAllUsersWithoutFilter();
              if (ds.Tables[0].Rows.Count > 0)
              {
                  foreach (DataRow dRow in ds.Tables[0].Rows)
                  {
      
                      string userName = dRow["UserName"].ToString();
                      string acctNo = dRow["AccountNumber"].ToString();
                      string question = dRow["SecretQuestion"].ToString();
                      string answer = dRow["SecretAnswer"].ToString();
      
                      if (userName == txtUsername.Text.ToString() && acctNo == txtAcctNo.Text.ToString() && question == cboQuestion.Text.ToString() &&  answer == txtAnswer.Text.ToString())
                      {
                           // if exist execute following code
      
                          startService.sendTokenizer(txtUsername.Text.ToString(), token);
                      //update database to change password to standard password
                      startService.inserUserActivity(txtUsername.Text.ToString(), txtAcctNo.Text.ToString(), "Password Reset Request", HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
                      startService.requestReset(txtUsername.Text.ToString(), txtAcctNo.Text.ToString(), token);
      
                      lblMessage.ForeColor = System.Drawing.Color.Green;
                      lblMessage.Text = "<br>We have sent an email to you for the instructions to reset your password. Please check your email.";
                      }
                      else
                      {
                        // id not exist then execute following code
      
                         this.lblMessage.ForeColor = System.Drawing.Color.Red;
                      this.lblMessage.Text = "<br><br>Error - Information cannot be found. Please check and try again. Make sure all the fields are correct.";
                      }
      
                  }              
      
              }
          }
          catch
          {
              lblError.Text = "There was an error occured while processing your request. Please try again later.";
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-18
        • 2012-08-10
        • 1970-01-01
        相关资源
        最近更新 更多