嗨,大家好,如果在下面的代码中找不到这样的记录,我将如何应用错误
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from CustTicket where CustmerContactNumber ='" + tbCcontact.Text.ToString() + "'";
db.ExeNonQuery(cmd);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
tbTest.Text = dr["CustmerName"].ToString();
}
}
答案 0 :(得分:3)
if (dt.Rows.Count == 0 )
{
// Here you can throw new Exception(); or something like that
MessageBox.Show("Some text", "Some title", MessageBoxButtons.OK, MessageBoxIcon.Error);
}