关于异常错误 [英] Regarding Exception Error

查看:86
本文介绍了关于异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#.net Windows应用程序中进行项目.我已经在textbox的textchanged事件中编写了一些计算代码.当在该特定textbox中更改了Text时,发生异常错误(即),已经有一个打开的DataReader与此命令相关联的命令必须先关闭.但是我已经关闭了所有阅读器和连接.我的代码是

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

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();
            }

推荐答案

con_str.Open();

当您每次文本更改时都尝试打开连接时,此行会导致问题.

将其放在TextChanged方法之外或置于if 条件中,以检查连接的状态,即连接是打开还是关闭.
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.


这篇关于关于异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆