2

在我的项目中,我从数据库中选择基于某些条件的用户 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();
 }

}

4

6 回答 6

2

作为索引号 从零开始你应该运行你的循环直到总行数减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++)
于 2013-05-07T09:58:56.330 回答
1

将您的for条件更改为for (int x = 0; x < 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();
    }
于 2013-05-07T10:09:36.743 回答
1

index比计数少一个,rows所以改变循环条件。rows 集合是基于零的索引,第一个元素的索引为零,最后一行的索引比行数少一个。

改变

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

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

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

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

}
于 2013-05-07T09:58:58.173 回答
0

请将查询更改为此

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

于 2013-05-07T10:03:23.037 回答
0

要么使用

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

或者

for (int x = 0; x <= dt.Rows.Count-1; x++)
于 2013-05-07T10:21:28.970 回答
0

改变你的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

于 2013-05-07T09:59:32.567 回答