在我的項目中,我選擇基於來自數據庫的某個條件的用戶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