Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void textBox5_TextChanged(object sender, EventArgs e)
        {
             objConn1.Open(); 
            int nQty = 0;
            int nActualQty;
                if (!int.TryParse(textBox5.Text, out nActualQty))
                   {
                    // Report problem to user - he typed wrong.
                    
                   }
         
            string sql3 = "select qty from dbo.RateMouldQuantity where ratechart= '" + comboBox5.SelectedValue.ToString() + "'";
            SqlCommand com = new SqlCommand(sql3, objConn1);
            SqlDataReader objQty = com.ExecuteReader();
           
            if (objQty.Read())
            {
                nQty = Convert.ToInt32(objQty["Qty"]);
            }
            if (nQty < nActualQty)
            {
                MessageBox.Show("Qty is greater");
                
            }
            textBox5.Clear();
            objConn1.Close();
            
        }
Posted
Updated 16-Dec-12 19:49pm
v2

Probably, it is to do with your
C#
if (!int.TryParse(textBox5.Text, out nActualQty))
   {
    // Report problem to user - he typed wrong.

   }
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:
C#
if (objConn1.State != ConnectionState.Open)
   {
   objConn1.Open();
   }

2) Use a try..finalize block each time you open the connection:
C#
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):
C#
private void textBox5_TextChanged(object sender, EventArgs e)
    {
    int nQty = 0;
    int nActualQty;
    if (!int.TryParse(textBox5.Text, out nActualQty))
        {
        // Report problem to user - he typed wrong.

        }
    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)
 
Share this answer
 
try this.

C#
private void textBox5_TextChanged(object sender, EventArgs e)
{
if( objConn1.State == ConnectionState.Open)
 {
   objConn1.Close();  
 }
 objConn1.Open(); 
 int nQty = 0;
 int nActualQty;
 if (!int.TryParse(textBox5.Text, out nActualQty))
 {
     // Report problem to user - he typed wrong.
 }
 string sql3 = "select qty from dbo.RateMouldQuantity where ratechart= '" + comboBox5.SelectedValue.ToString() + "'";
 
 SqlCommand com = new SqlCommand(sql3, objConn1);
SqlDataReader objQty

 try
 {
    objQty = com.ExecuteReader();
           
    if (objQty.Read())
    {
      nQty = Convert.ToInt32(objQty["Qty"]);
    }
    if (nQty < nActualQty)
   {
      MessageBox.Show("Qty is greater");
   }
 } catch(Exception exp){}
 if( objQty != null)
 {
  objQty.Close(); // You need to close the Reader class once you finished using it.
 }
 textBox5.Clear();
 objConn1.Close();
            
}
 
Share this answer
 
C#
string sql3 = "select qty from dbo.RateMouldQuantity where ratechart= '" + comboBox5.SelectedValue.ToString() + "'";
            SqlCommand com = new SqlCommand(sql3, objConn1);
            SqlDataReader objQty = com.ExecuteReader();
           
            if (objQty.Read())
            {
                nQty = Convert.ToInt32(objQty["Qty"]);
            }
            if (nQty < nActualQty)
            {
                MessageBox.Show("Qty is greater");
                
            }
            textBox5.Clear();
            com.Connection.Close();  // add this...
            objConn1.Close();
 
Share this answer
 
Comments
Master Vinu 17-Dec-12 1:59am    
no dear ...not working same error when debug the code on objconnection.open()
[no name] 17-Dec-12 2:09am    
ok, use try catch..finally...blocks
Master Vinu 17-Dec-12 2:38am    
thx but still there is error
Master Vinu 17-Dec-12 2:36am    
give folloeing errro on :using (SqlDataReader objQty = com.ExecuteReader())

There is already an open DataReader associated with this Command which must be closed first.
[no name] 17-Dec-12 2:41am    
private void textBox5_TextChanged(object sender, EventArgs e)
{
int nQty = 0;
int nActualQty;
string sql3 = "select qty from dbo.RateMouldQuantity where ratechart= '" + comboBox5.SelectedValue.ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(sql3, objConn1);
DataTable dt = new DataTable();
da.fill(dt);

foreach(DataRow dr in dt.Rows)
{
nQty = int.Parse(dr["Qty"].ToString());
if (nQty < nActualQty)
{
MessageBox.Show("Qty is greater");

}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900