Messagebox to show no records found in search of sql database to gridview

  • Thread starter Thread starter wgraulich
  • Start date Start date
W

wgraulich

Guest
I have a search button that searches a Sql table and it works fine. What I am trying to do is have a message box pop when the search returns 0 rows. At the end of my search I added an "if" statement if the datagridview row count is 0, but when I search and get no returns, the message box does not appear. How can I get an message box to appear when there are no returns?
private void Searchbtn_Click(object sender, EventArgs e)
{
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * from Overnight_Parking Where (Dispatcher like @Search or Callers_Name like @Search or Callers_Phone like @Search or City like @Search or Address like @Search or Reason like @Search or Description like @Search)";

cmd.Parameters.AddWithValue("@Search", "%" + Search.Text + "%");

cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
if (dataGridView1.RowCount == 0)
MessageBox.Show("No Records Found");

con.Close();
//textbox clear code
Search.Text = "";

}
}

Continue reading...
 
Back
Top