Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Invalid attempt to call HasRows when reader is closed. please guide me

C#
try
            {
                con = new SqlConnection(ConfigurationManager.ConnectionStrings["TextItConnectionString"].ConnectionString);
                using (con)
                {
                    con.Open();
                    Library.writeErrorLog("connection build and open");
                    SqlCommand cmd = con.CreateCommand();
                    using (cmd)
                    {
                        cmd.CommandText = "Select [name] From [dbo].[Users]";
                        SqlDataReader reader = cmd.ExecuteReader();
                        using (reader)
                        {
                            user.dt.Load(reader);
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    Library.writeErrorLog(reader.GetString(0));
                                }
                            }
                            else
                                Library.writeErrorLog("no rows");
                            reader.Close();
                            con.Close();
                        }
                    }
                }
                //SqlDataAdapter adap = new SqlDataAdapter("Select [name] From [dbo].[Users]", con);
                //adap.Fill(user.dt);
            }
            catch (Exception ex)
            {
                Library.writeErrorLog(ex);
            }


What I have tried:

read on alot of forums that dont close the connection before reading. i have closed in the end. i have also tried removing close connection line but having same error
Posted
Updated 6-Jun-16 1:57am
Comments
Shahin Khorshidnia 6-Jun-16 7:52am    
Maybe the reader has already closed. Perhaps it occurs after ExecuteReader() for some unexpected problems. Check ExecuteReader()
Member 12567974 6-Jun-16 7:54am    
whats the problem in ExecuteReader. I cant get the problem in it
Shahin Khorshidnia 6-Jun-16 8:44am    
I don't know to be honest. Why not using a simple breakpoint and watching inside the returned value? For example check the IsClosed property and what not. Besides, it works as yield return. Have you tried not to use using{} block this way?

1 solution

A DataReader is a one direction connection - once you read a row, you can;t go back to teh previous one.
So when you use DataTable.Load to read from the SqlDataReader, it closes the Reader when it's finished with all data.
Why are you trying to use the same DataReader to read all the data twice? Instead, loop through the DataTAble result to provide your log info.
Better, use an SqlDataAdapter and load the datatable from that instead, as it opens teh connection object for you.
And usingblocks shouldn't be written like that:
C#
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TextItConnectionString"].ConnectionString))
{
    con.Open();
    Library.writeErrorLog("connection build and open");
    using (SqlCommand cmd = con.CreateCommand())
    {

Is the better method as it ensures the variable is Disposed when it goes out of scope.
 
Share this answer
 

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