无效试图调用元数据时,读写器被关闭? [英] Invalid attempt to call MetaData when reader is closed?

查看:229
本文介绍了无效试图调用元数据时,读写器被关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行的,如果else语句了一个DataReader的查询表中的数据,并激活/取消激活页面上的一些控件。我实现了一个using语句,当我关闭块自动关闭连接和读者,但我仍然得到读者关闭错误每个我的if else语句。少了什么东西?低于code:

 字符串comnt =SELECT StatusId从提交WHERE SubmissionId =+ X;
    使用(SqlConnection的editConn =新的SqlConnection(的connectionString))
    {
        editConn.Open();        使用(的SqlCommand statCmd =新的SqlCommand(comnt,editConn))
        {
            SqlDataReader的博士= statCmd.ExecuteReader();
            dr.Read();
            如果(dr.GetInt32(0)大于0)
            {
                PanelComment.Visible = TRUE;
                PanelQuote.Visible = FALSE;
                LnbFid.Visible = FALSE;
                LnbCrim.Visible = FALSE;
                LnbEo.Visible = FALSE;
                LnbEpl.Visible = FALSE;
                LnbNot.Visible = FALSE;
                LnbPriv.Visible = FALSE;
                LnbPub.Visible = FALSE;            }
            其他
            {
                PanelComment.Visible = FALSE;
            }        }


解决方案

您查询没有得到任何结果回来。习惯了下面的结构,如果你不知道,如果你的查询将返回任何数据:

 而(dr.Read())//将同时存在要读取的数据返回true。
{
    ...
}

I'm running an if else statement off of a datareader to query table data and activate/de-activate some controls on a page. I implemented a using statement to automatically close the connection and the reader when I close the block, but I still get the reader is closed error on each of my if else statements. What's missing? Code below:

string comnt = "SELECT StatusId FROM Submission WHERE SubmissionId =" + x;


    using (SqlConnection editConn = new SqlConnection(connectionString))
    {
        editConn.Open();

        using (SqlCommand statCmd = new SqlCommand(comnt, editConn))
        {
            SqlDataReader dr = statCmd.ExecuteReader();
            dr.Read();
            if (dr.GetInt32(0) > 0)
            {
                PanelComment.Visible = true;
                PanelQuote.Visible = false;
                LnbFid.Visible = false;
                LnbCrim.Visible = false;
                LnbEo.Visible = false;
                LnbEpl.Visible = false;
                LnbNot.Visible = false;
                LnbPriv.Visible = false;
                LnbPub.Visible = false;

            }
            else
            {
                PanelComment.Visible = false;
            }

        } 

解决方案

Your query is not getting any results back. Get used to the following construct if you are not sure if your query will return any data:

while (dr.Read()) //will return true while there is data to be read.
{
    ...
}

这篇关于无效试图调用元数据时,读写器被关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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