2

I have a method in C# which should get some data from mysql and then insert this data in another table.
The problem is that no matter what I try i get another datareader is open.
I tried using the using statement, but with no success, maybe I do it wrong.
How to do that?

public void MethodName(List<string> objects)
{
    foreach(string Object in objects){
        SQLActions.Initialize();
        SQLActions.SQL_Open();
        MySqlDataReader queryData = SQLActions.SQL_Query("SELECT...query...");

        MySqlDataReader objectsData = SQLActions.SQL_Query("Another select....");
        SQLActions.SQL_NonQuery("Insert...");
        while(queryData.Read()){
            SQLActions.SQL_NonQuery("Another Insert...");
        }
        SQLActions.SQL_Close();
    }
}
Paul Reed
  • 133
  • 2
  • 10

1 Answers1

2

You are probably using the same connection for the DataReader and the ExecuteNonQuery. This is not supported, according to MSDN: http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.80).aspx

Check this:

C# mySQL There is already an open DataReader associated

From Microsoft:

You should always call the Close method when you have finished using the DataReader object.

Community
  • 1
  • 1
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82