This is the edited code I removed try catch to see where i am getting the exceptions. I alse marked where I edited the code. This code is getting on my nerves I have been googling this problem and tried everything i found. I checked every connection other then this code there seems to be no problem with them. each and every connection is closed.
queryString = "SELECT * FROM product WHERE prd_code = @c OR prd_name=@pn ";
SqlCommand command = new SqlCommand(queryString, con);
command.Parameters.AddWithValue("@c", Convert.ToInt32(txtpCode.Text));
command.Parameters.AddWithValue("@pn", txtpName.Text.ToString());
con.Open();
//edited here
using (SqlDataReader dr = command.ExecuteReader())
{
// and here
if (dr != null && fr.HasRows && dr.Read() == true)
{
if (txtpCode.Text == "")
{
txtpCode.Text = dr["prd_code"].ToString();
}
else if (txtpName.Text == "")
{
txtpName.Text = dr["prd_name"].ToString();
}
txtpCompany.Text = dr["prd_company"].ToString();
txtUnitPrice.Text = dr["prd_price"].ToString();
txtDiscount.Text = dr["prd_dis"].ToString();
txtFinalRate.Text = dr["prd_final"].ToString();
}
else
{
MessageBox.Show("No such record exists");
if (txtpName.Text == "")
{
txtpCode.Text = "";
}
else if (txtpCode.Text == "")
{
txtpName.Text = "";
}
con.Close();
}
}
}