如果在此代码中找不到记录,如何添加异常错误

时间:2017-07-31 21:55:33

标签: c# sql windows-forms-designer

嗨,大家好,如果在下面的代码中找不到这样的记录,我将如何应用错误

    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();
        }
     }

1 个答案:

答案 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);
}