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