Probably, it is to do with your
if (!int.TryParse(textBox5.Text, out nActualQty))
{
}
Section - you don't show it, but if you do not close the connection there, it will remain open (since I assume you do a return in that bit).
There are two alternatives here:
1) Use this code for the open:
if (objConn1.State != ConnectionState.Open)
{
objConn1.Open();
}
2) Use a try..finalize block each time you open the connection:
try
{
objConn1.Open();
...
}
finally
{
if (objConn1.State == ConnectionState.Open)
{
objConn1.Close();
}
}
I would do both, and also move the validation code outside the try block (there is no point in opening in the connection if you aren't going to use it):
private void textBox5_TextChanged(object sender, EventArgs e)
{
int nQty = 0;
int nActualQty;
if (!int.TryParse(textBox5.Text, out nActualQty))
{
}
else
{
try
{
if (objConn1.State != ConnectionState.Open)
{
objConn1.Open();
}
string sql3 = "select qty from dbo.RateMouldQuantity where ratechart= '" + comboBox5.SelectedValue.ToString() + "'";
using (SqlCommand com = new SqlCommand(sql3, objConn1))
{
using (SqlDataReader objQty = com.ExecuteReader())
{
if (objQty.Read())
{
nQty = Convert.ToInt32(objQty["Qty"]);
}
if (nQty < nActualQty)
{
MessageBox.Show("Qty is greater");
}
}
}
textBox5.Clear();
}
finally
{
objConn1.Close();
}
}
}
(I have also thrown in a couple of
using
blocks, because you should dispose SQL commands and readers)