Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use

C#
MySqlConnection addh = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

        addh.Open();

MySqlCommand myCommand = new MySqlCommand(myqry, addh);

and then
C#
MySqlDataReader myDataReader;
        myDataReader = myCommand.ExecuteReader();

if (myDataReader.HasRows)
 {
     while (myDataReader.Read())
     {


foreach(string ede in array)
{
MySqlCommand cmd = new MySqlCommand(amenity, addh);

                        int insert_result = cmd.ExecuteNonQuery();


}



}

myDataReader.Close();

addh.Close();

But i get error as
C# mySQL There is already an open DataReader associated with this Connection which must be closed first


what is issue
Posted
Updated 14-Jan-14 0:47am
v3
Comments
navin ks 14-Jan-14 6:41am    
use another connection for datareader as the error says same
maulikshah1990 14-Jan-14 6:42am    
in above code..pls make changes as example and give me proper code

1 solution

You cannot use the same connection to perform any other database operation while there is an open DataReader associated with it: it would mess up the data returned from the database. It's just like trying to change a List<string> from inside a foreach loop which is working on the list: it would cause problems so it isn't allowed.
Either create a new connection and use that, or save the data to do the operations after the reader has been closed.
 
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