Click here to Skip to main content
15,919,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";
               SqlConnection sqlConnection = new SqlConnection(connectionstring);
               SqlCommand cmd = new SqlCommand();
               SqlDataReader reader,reader1;
               DataSet ds = new DataSet();
               cmd.CommandText = "select * from Empdetails";
               cmd.CommandType = CommandType.Text;
               cmd.Connection = sqlConnection;
               sqlConnection.Open();
               reader = cmd.ExecuteReader();
               reader1 = cmd.ExecuteReader();
               if (reader.HasRows)
               {
                   System.IO.StreamWriter sw_In = new System.IO.StreamWriter(@"C:\Users\God\Desktop\DataDump\IN_Excel.xls");
                   if (reader.Read())
                   {
                       string status = reader[6].ToString();
                       if (status == "1") ;
                       {
                           for (int i = 0; i < reader.FieldCount; i++)
                           {
                               sw_In.AutoFlush = true;
                               sw_In.Write(reader[i].ToString() + "\t");
                           }
                           sw_In.Write("\n");
                       }
                   }
                   reader.Close();
               }

                   if (reader1.HasRows)
                    {
                           System.IO.StreamWriter sw_Out = new System.IO.StreamWriter(@"C:\Users\God\Desktop\DataDump\OUT_Excel.xls");
                      if (reader1.Read())
                       {
                       string status1 = reader1[6].ToString();
                      if(status1 == "2");
                         {
                           for (int i = 0; i < reader1.FieldCount; i++)
                           {
                               sw_Out.AutoFlush = true;
                               sw_Out.Write(reader1[i].ToString() + "\t");
                           }
                              sw_Out.Write("\n");
                        }

                      }
                      reader1.Close();
                  }
               sqlConnection.Close();


When i run the above code shows error as follows

There is already an open DataReader associated with this Command which must be closed first.

i already close DataReader

for that DataReader close code as follows

reader.Close();

Whehter i close the Datareader line is correct or need to mention that Reader close in other line of my code.

What I have tried:

There is already an open DataReader associated with this Command which must be closed first.
Posted
Updated 15-Aug-16 3:18am
v2

You have two readers for the same command

C#
reader = cmd.ExecuteReader();
reader1 = cmd.ExecuteReader();


Readers are only for data that you want to go through once. As you need to go through the data twice use a DataSet instead.
 
Share this answer
 
So, your problem is:
C#
reader = cmd.ExecuteReader();
reader1 = cmd.ExecuteReader();


The exception tells you that this is a problem. There is no reason to do this.

Also, you're leaving a whole bunch of undisposed handles floating around when you do this. The rule of thumb is: If something implements IDisposable, dispose of it when you're done with it.

C#
using(var reader = cmd.ExecuteReader())
{
...
}
 
Share this answer
 
Quote:
i already close DataReader

You don't understand that reader1 is opened at
C#
reader1 = cmd.ExecuteReader();

which is before you close reader.

Rule of thumb: the error message exist for reason, it imply that you are wrong, just have to find why.
 
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