Dhary Almousa
Dhary Almousa

Reputation: 11

How to add an exception error if record is not found in this code

Hi guys as the tittle says how would i apply an error if no such record is found in this code below

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

Upvotes: 0

Views: 179

Answers (1)

Roman Marusyk
Roman Marusyk

Reputation: 24609

You mean?

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

Upvotes: 3

Related Questions