我正在尝试确定如何检查我的
我尝试将此代码放置在几个不同的位置,但如果没有记录返回,则似乎没有任何位置触发消息框。
SqlDataReader
是否为 null 或没有行(表示预订不存在),然后显示一个消息框。出于某种原因,当我调试时它会在不返回结果的情况下跳出 While dr.Read())
代码。我尝试将此代码放置在几个不同的位置,但如果没有记录返回,则似乎没有任何位置触发消息框。
if (dr.GetValue(0) == DBNull.Value || !dr.HasRows)
{
MessageBox.Show("Reservation Number Does Not Exist","Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
(read records)
}
我的代码...
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = "usp_StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@regnum", regnumber);
using (SqlDataReader dr = cmd.ExecuteReader())
{
//Loop through all the rows, retrieving the columns you need.
while (dr.Read())
{
lblConf.Text = dr.GetValue(0).ToString();
lblName.Text = dr.GetValue(1).ToString() + "," + dr.GetValue(2);
lblCompany.Text = dr.GetValue(3).ToString();
lblStatus.Text = dr.GetValue(4).ToString();
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection! ");
}