Click here to Skip to main content
15,884,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
I have written function for getting the datareader using connection and query but some time is gives error , "{System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first."
Please help for this
Thanks
vilas
C#
public static DbDataReader GetDataReader(DbConnection myConnection, string strQry)
        {
            DbDataReader ObjmyReader0 = null;
            DbDataReader ObjmyReader = null;


            string RetVal = string.Empty;
            DbCommand myCommand = null;
             myCommand = (DbCommand)myConnection.CreateCommand();
            // myCommand.CommandText = "SELECT * FROM Dept";
            myCommand.CommandText = strQry;
            if (myConnection.State == ConnectionState.Closed || myConnection.State == ConnectionState.Broken) {
                myConnection.Open();
            }

            try
            {
                if (ObjmyReader != null)
                {
                    ObjmyReader.Close();
                }

                ObjmyReader = myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            }
            catch (Exception rdExc)
            {
                if ((ObjmyReader != null) && (ObjmyReader.IsClosed == false))
                {
                    ObjmyReader.Close();
                    ObjmyReader = myCommand.ExecuteReader();
                }
            }
            finally
            {
                myCommand.Dispose();
                ObjmyReader0 = ObjmyReader;
               
               // ObjmyReader.Close();
            }

            return ObjmyReader0;
           
        }
Posted
Updated 7-Dec-12 23:22pm
v3
Comments
choudhary.sumit 7-Dec-12 6:10am    
close your reader at the very top line in GetDataReader(..) method.
Mogya 7-Dec-12 7:30am    
ok thanks ,
i have tried to close reader first before calling getdatareader method ,
But at the time of dynamic calling i.e in while loop it is fail
Jibesh 7-Dec-12 20:13pm    
The above code close the connection only when ObjmyReader != null which never gets executed because its always null and gets assigned after this line.

do check the ConnectionState.Opened means close the connection opened by the reader. or close the Reader itself with last readerObject not with the newly created one.
You have not closed the connection myConnection.
In finally block, write like myConnection.Close();.
thursunamy 8-Dec-12 5:34am    
Hi,
Write to your connection string MultipleActiveResultSets=true

Try the following implementation (I have not tested this):

C#
public void TestGetDataReader()
        {
            string strDbConn = "Database connection string";
            string strQry = "Database Query";

            using (DbConnection dbConn = new SqlConnection(strDbConn))
            {
                dbConn.Open();
                using (DbDataReader dr = GetDataReader(dbConn, strQry))
                {
                    // Use results from data reader here
                    // using will automatically close the data reader once you have finished with the results
                }
            }
        }

        public static DbDataReader GetDataReader(DbConnection myConnection, string strQry)
        {
            using (DbCommand cmd = (DbCommand)myConnection.CreateCommand())
            {
                cmd.CommandText = strQry;

                if (myConnection.State == ConnectionState.Closed || myConnection.State == ConnectionState.Broken)
                {
                    myConnection.Open();
                }

                return cmd.ExecuteReader();
            }
        }
 
Share this answer
 
I have use dataTable and load the Idatareader and after this create the datareader ,

there after i am easily used this function with multiple loop.

C#
public static IDataReader GetDataReader(DbConnection myConnection, string strQry)
        {
            IDataReader ObjmyReader0 = null;
            IDataReader ObjmyReader = null;

            var dt = new DataTable();

            string RetVal = string.Empty;
            DbCommand myCommand = null;
             myCommand = (DbCommand)myConnection.CreateCommand();
            // myCommand.CommandText = "SELECT * FROM Dept";
            myCommand.CommandText = strQry;
            if (myConnection.State == ConnectionState.Closed || myConnection.State == ConnectionState.Broken) {
                myConnection.Open();
            }

            try
            {
                if (ObjmyReader != null)
                {
                    if (ObjmyReader.IsClosed == false) 
                    {
                        ObjmyReader.Close();
                    }
                }

                ObjmyReader = (IDataReader)myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                //var dt = new DataTable();
                dt.Load(ObjmyReader);
               // dt.CreateDataReader();
               // return dt.CreateDataReader();

              
            }
            catch (Exception rdExc)
            {
                if ((ObjmyReader != null) && (ObjmyReader.IsClosed == false))
                {
                    ObjmyReader.Close();
                    ObjmyReader = (IDataReader)myCommand.ExecuteReader();
                }
            }
            finally
            {
                myCommand.Dispose();
                ObjmyReader0 = ObjmyReader;
               
               // ObjmyReader.Close();
            }

            //return ObjmyReader0;
            return dt.CreateDataReader();
            //return (IDataReader)Convert.ChangeType(ObjmyReader0, typeof(IDataReader));
            ObjmyReader0.Dispose();
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900