当datareader关闭时,SqlDataReader返回无效尝试 [英] SqlDataReader returns invalid attempt when datareader closed- reg

查看:110
本文介绍了当datareader关闭时,SqlDataReader返回无效尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void BindReader(string storedproc,string[] param)
       {
           clear();
           reader = ppt.ParamReader(storedproc,param ); // Here searching a Branch Name and binding values in matching text box
           while (reader.Read())
           {
               lbl_ID.Text = reader["BRANCH_ID"].ToString();
               txt_Address.Text = reader["BRANCH_ADDRESS"].ToString();
               txt_Branch_Phone.Text = reader["BRANCH_PHONE"].ToString();
               txt_CST_Number.Text = reader["CST_NUMBER"].ToString();
               txt_tin_number.Text = reader["TIN_NUMBER"].ToString();
               cmb_Branch_Name.Text = reader["BRANCH_NAME"].ToString();
           }
           reader.Close();
       }


public SqlDataReader ParamReader(string sp, string[] pp)
{
    if (_connection.State == ConnectionState.Open)
    {
        _connection.Close();
    }
    _connection.ConnectionString = setConnection();
    try
    {
        _connection.Open();
        cmd = parameters(sp, pp, _connection);// Calls Parameters Method to load the Parameters in the command
        reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }
    catch (Exception e)
    {
        throw new Exception(e.Message);
    }
    return reader;
}



<code>
此代码是第一次工作.如果再次调用此方法,则在关闭数据读取器时将返回错误,例如无效尝试.
是什么原因?



<code>
This code is working at very first time. if I call this method again it returns error like invalid attempt when data reader is closed.
what is the reason? what should i do for this to over come this?

推荐答案

您要在此处关闭连接.
You are closing your connection here.
if (_connection.State == ConnectionState.Open)    {        _connection.Close();    }



这就是为什么下次运行代码时会出现此错误的原因.



That is why you get this error when you run your code the next time.




我认为您已经声明了Reader全局并且在BindReader方法中关闭了.
并尝试在cmd.ExecuteReader(CommandBehavior.CloseConnection)中打开阅读器.

因此,第一个实例与其之前的新实例一起.
Hi ,

I think you have declare reader global and close in BindReader method.
And try open reader in cmd.ExecuteReader(CommandBehavior.CloseConnection).

So first instance with it new instance before this.


这篇关于当datareader关闭时,SqlDataReader返回无效尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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