satırının bulunup bulunmadığını nasıl kontrol ederim SqlDataReader
numaranın boş olup olmadığını nasıl kontrol edeceğimi (ya da hiç bir satır bulunmadığını) anlamaya çalışıyorum ve sonra bir ileti kutusu görüntüler. Herhangi bir nedenle, While dr.Read())
koduna eriştiğinde hata ayıkladığımda, bir geri dönüş sonucu yoksa, adım atılır. ...SQLDataReader'ın
Birkaç farklı yerlerde bu kodu koyarak denedim ama hiçbir kayıt
if (dr.GetValue(0) == DBNull.Value || !dr.HasRows)
{
MessageBox.Show("Reservation Number Does Not Exist","Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
(read records)
}
Benim kod iade edilirse hiçbiri messagebox ateşlemesi gibi görünüyor
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! ");
}
Döngününüzü olduğu gibi tutun, ancak bir sarın Eğer etrafında bir Bildirim Varsa (dr.HasRows) {} – MethodMan