0) Put the code into a try/catch block.
try
{
con.Open();
SqlDataAdapter comm = new SqlDataAdapter("SearchMain", con);
comm.SelectCommand.CommandType = CommandType.StoredProcedure;
comm.SelectCommand.Parameters.AddWithValue("@date", searchtbx.Text.Trim());
comm.SelectCommand.Parameters.AddWithValue("@yearlvl", searchtbx.Text.Trim());
comm.SelectCommand.Parameters.AddWithValue("@complaint", searchtbx.Text.Trim());
comm.SelectCommand.Parameters.AddWithValue("@medicine", searchtbx.Text.Trim());
comm.SelectCommand.Parameters.AddWithValue("@fname", searchtbx.Text.Trim());
DataTable dt = new DataTable();
dataGridView1.DataSource = dt;
comm.Fill(dt);
con.Close();
}
catch (Exception ex)
{
}
1) USE.THE.DEBUGGER.
2) If you use using statements, you don't have to worry about manually closing the conmection.
SqlConnection con;
SqlAdapter comm;
try
{
using (con = new SqlConnection(conectionstring))
{
con.Open();
using (comm = new SqlDataAdapter("SearchMain", con))
{
}
}
}
catch (Exception ex)
{
}
3) Your problem is probably going to be one of these (we don't know which because "has an error" sucks as as problem description:
a) The connection to the database failed
b) The stored proc wasn't found
c) The parameter list is somehow incorrect.