【问题标题】:How to Discard Null Values in Data Table when Checking with IF Condition inside a For Loop在 For 循环中使用 IF 条件检查时如何丢弃数据表中的空值
【发布时间】:2014-09-30 15:22:07
【问题描述】:

在我的项目中,我根据数据库中的某些条件选择用户 ID,并将其保存在数据表中,并使用基于条件的用户输入的 ID 检查它,只有 5 行将被提取,但在循环和 IF 条件下它正在检查为空的第 6 行,因此它抛出异常“第 6 位没有行”我的代码是

protected void Button1_Click(object sender, EventArgs e)
 {
   SqlConnection con1 = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=Eval;User ID=sa;Password=sa@123");         
    try
    {

     string qry = "Select Userid from Faculty where Flag='A'";
     SqlCommand cmd = new SqlCommand(qry,con1);
     con1.Open();
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataTable dt = new DataTable();
     da.Fill(dt);
     for (int x = 0; x <= dt.Rows.Count; x++)
     {
        //DataRow row = dt.Rows[i];
        //object ID = row[0];
        //if (ID != null && !String.IsNullOrEmpty(ID.ToString().Trim()))
        dt.Select("Userid is Not Null");

        if (TextBox1.Text == dt.Rows[x]["Userid"].ToString())
        {
          lblMessage.Text = string.Empty;

          Panel1.Visible = true;

        }
        else
        {
          lblMessage.Visible = true;
          lblMessage.ForeColor = System.Drawing.Color.Red;

          lblMessage.Text = "Invalid Userid or UserId does not Exist in the Database !!!";
         }

    }

  }
 finally
 {
   con1.Close();
   con1.Dispose();
 }

}

【问题讨论】:

    标签: c# sql-server


    【解决方案1】:

    作为索引号。从零开始你应该运行你的循环直到总行数减去1。在你的情况下你有5行所以dt.Rows.Count会给你5,循环从起始索引0开始,所以如果你写它会运行到5 dt.Rows.Count,因此您应该编写 dt.Rows.Count-1 以便循环仅运行到第 5 行,这最终是索引为 4 的最后一行

     for (int x = 0; x <= dt.Rows.Count-1; x++)
    

    【讨论】:

    • 我很高兴,很好,如果它对你有用,请接受答案 rajesh
    【解决方案2】:

    要么将for 条件更改为for (int x = 0; x &lt; dt.Rows.Count; x++),要么使用foreach 循环

        try
        {
    
            string qry = "Select Userid from Faculty where Flag='A'";
            SqlCommand cmd = new SqlCommand(qry,con1);
            con1.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            foreach (DataRow dc in dt.Rows)
            {
                if (TextBox1.Text == dc["Userid"].ToString())
                {
                    lblMessage.Text = string.Empty;
    
                    Panel1.Visible = true;
    
                }
                else
                {
                    lblMessage.Visible = true;
                    lblMessage.ForeColor = System.Drawing.Color.Red;
    
                    lblMessage.Text = "Invalid Userid or UserId does not Exist in the Database !!!";
                }
    
            }
    
        }
        finally
        {
            con1.Close();
            con1.Dispose();
        }
    

    【讨论】:

      【解决方案3】:

      您的indexrows 少一,因此请更改循环条件。 rows 集合是基于零的索引,第一个元素的索引为零,最后一行的索引比行数小一。

      改变

      for (int x = 0; x <= dt.Rows.Count; x++)
      

       for (int x = 0; x < dt.Rows.Count; x++)
      

      您可以使用 Select 过滤用户 ID 不为空的行。

      DataRow[] dataRows = dt.Select("Userid is Not Null");
      foreach (DataRow dataRow in dataRows )
      {
      
      }
      

      【讨论】:

        【解决方案4】:

        如下改变你的for循环

         for (int x = 0; x < dt.Rows.Count; x++)
        

        并如下更改您的 sql

        string qry = "Select Userid from Faculty where Flag='A' and Userid is Not Null";
        

        您不需要通过代码进行用户ID检查

        【讨论】:

          【解决方案5】:

          请将查询更改为此

          从 Flag='A' AND Userid IS NOT NULL 的学院中选择 Userid

          【讨论】:

            【解决方案6】:

            随便用

             for (int x = 0; x < dt.Rows.Count; x++)
            

            for (int x = 0; x <= dt.Rows.Count-1; x++)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-06-16
              • 2023-01-18
              • 1970-01-01
              • 2016-01-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多