在我的项目中,我选择基于来自数据库的某个条件的用户ID并将其保存在数据表中,并使用用户输入的ID进行检查一旦条件只有5行会,但在循环和IF条件它与第6行是空,因此它抛出异常检查中获取“没有排在第6位”我的代码是如何在For循环中使用IF条件进行检查时在数据表中丢弃空值
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=Eval;User ID=sa;[email protected]");
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();
}
}
感谢十亿工作... – Rajesh 2013-05-07 10:11:27
好,我的荣幸,好,接受答案,如果它为你工作rajesh – Anuj 2013-05-07 10:12:18