无效试图调用时,请阅读阅读器关闭 [英] Invalid attempt to call Read when reader is closed

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

问题描述

那种

我挣扎试图让这个数据网格进行绑定。每次我跑我的code,我得到一个错误消息指出,无效的尝试调用read时读取器已关闭。我看不出我将结束我的读者。你能帮帮我吗?我的code加载数据网格是如下:

 保护无效LoadGrid()
        {
            使用(SqlConnection的康恩=新的SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings [职训局]的ConnectionString。
                conn.Open();


                字符串SQL =选择roi_tracking *;
                使用(CMD的SqlCommand =新的SqlCommand(SQL,康涅狄格州))
                {
                    使用(SqlDataReader的sqlReader = cmd.ExecuteReader())
                    {

                        gridROI.DataSource = sqlReader;
                        gridROI.DataBind();

                        sqlReader.Dispose();
                        cmd.Dispose();
                    }
                }

            }
        }
 

解决方案

您不能使用SqlDataReader作为数据源为DataGrid。

从MSDN:

  

有一个数据源必须是实现无论是集合   System.Collections.IEnumerable接口(如   System.Data.DataView,System.Collections.ArrayList,或   System.Collections.Generic.List(对T))或IListSource接口的到   结合从BaseDataList类派生的控制。

数据源属性: <一href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedatalist.datasource.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedatalist.datasource.aspx

SqlDataReader对象: 的http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

绑定查询结果到您的DataGrid中的一个常见的​​方法是使用SqlDataAdapter来填充数据表或数据集,然后将绑定到你的DataGrid.DataSource:

 保护无效LoadGrid()
    {
        使用(SqlConnection的康恩=新的SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings [职训局]的ConnectionString。
            conn.Open();

            字符串SQL =选择roi_tracking *;
            使用(CMD的SqlCommand =新的SqlCommand(SQL,康涅狄格州))
            {
                SqlDataAdapter的适配器=新的SqlDataAdapter();
                adapter.SelectCommand = CMD;

                adapter.Fill((数据表)的结果);
                gridROI.DataSource =结果;
            }

        }
    }
 

I'm kind of struggling trying to get this datagrid to bind. Everytime I run my code, I get an error message stating, "Invalid attempt to call Read when reader is closed". I don't see where I am closing my reader. Can you please help me? My code for loading the datagrid is below:

protected void LoadGrid()
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["VTC"].ConnectionString;
                conn.Open();


                string sql = "select * from roi_tracking";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    using (SqlDataReader sqlReader = cmd.ExecuteReader())
                    {

                        gridROI.DataSource = sqlReader;
                        gridROI.DataBind();

                        sqlReader.Dispose();
                        cmd.Dispose();
                    }
                }

            }
        }

解决方案

You can't use a SqlDataReader as a DataSource for a DataGrid.

From MSDN:

A data source must be a collection that implements either the System.Collections.IEnumerable interface (such as System.Data.DataView, System.Collections.ArrayList, or System.Collections.Generic.List(Of T)) or the IListSource interface to bind to a control derived from the BaseDataList class.

Datasource property: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedatalist.datasource.aspx

SqlDataReader: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

A common methodology to bind the query results to your datagrid would be to use a SqlDataAdapter to fill a DataTable or DataSet and then bind that to your DataGrid.DataSource:

protected void LoadGrid()
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["VTC"].ConnectionString;
            conn.Open();

            string sql = "select * from roi_tracking";
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;

                adapter.Fill((DataTable)results);
                gridROI.DataSource = results;
            }

        }
    }

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

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