Invalid attempt to call Read when reader is closed

↘锁芯ラ 提交于 2019-12-24 07:13:05

问题


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

            }
        }

回答1:


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

        }
    }



回答2:


In addition to what pseudocoder said, you wouldn't want to bind to a SqlDataReader anyway: the connection to the database will remain open so long as the reader instance exists.

You definitely want to deserialize the data into some other disconnected data structure so that you release the connection back into the pool as quickly as possible.



来源:https://stackoverflow.com/questions/8578659/invalid-attempt-to-call-read-when-reader-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!