Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm doing project in C#.net windows application.I've wriitten somecalculation code in textchanged event of textbox.When Text is changed in that paricular textbox, Exception Error occurs (ie) There is already an open DataReader associated with this Command which must be closed first.But I've closed all reader and Connection. My Code is
C#
private void BL_DueAmttxt_TextChanged(object sender, EventArgs e)
        {

                con_str.Open();
                cmd = new SqlCommand("select isnull(count(BL_BalAmt),0) from Bill where BL_Lno='" + BL_LnoCombo.Text + "'", con_str);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    int count = int.Parse(dr[0].ToString());
                    //float count = dr.GetValue(0);

                    if (count == 0)
                    {
                        try
                        {
                                float interest = float.Parse(BL_Inttxt.Text);
                                float DAmt = float.Parse(BL_DueAmttxt.Text);
                                float TotAmt = interest + DAmt;
                                BL_TotAmttxt.Text = TotAmt.ToString();
                                float SAmt = float.Parse(BL_LAmttxt.Text);
                                float BalAmt = SAmt - DAmt;
                                BL_BalAmttxt.Text = BalAmt.ToString();
                        }
                        catch (Exception ex)
                        { }
                    }
                    else
                    {
                        cmd = new SqlCommand("select top 1 BL_BalAmt-'" + BL_DueAmttxt.Text + "' from Bill where BL_Lno='" + BL_LnoCombo.Text + "' order by BL_Date Desc", con_str);
                        dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            try
                            {
                                {
                                    BL_BalAmttxt.Text = dr.GetValue(0).ToString();
                                    float interest = float.Parse(BL_Inttxt.Text);
                                    float DAmt = float.Parse(BL_DueAmttxt.Text);
                                    float TotAmt = interest + DAmt;
                                    BL_TotAmttxt.Text = TotAmt.ToString();
                                }
                            }
                            catch (Exception ex)
                            { }
                        }
                        dr.Close();
                    }
                }
                dr.Close();
                con_str.Close();
            }
Posted

1 solution

con_str.Open();

This line causes the problem as you are trying to open the connection every time the text changes.

Either put this outside the TextChanged method or put this in an if condition that checks the state of the connection i.e. whether it is open or closed.
 
Share this answer
 
Comments
Kasson 27-Dec-10 3:47am    
Good Call.

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