none
I/O Error system invalid operation on datatable RRS feed

  • Question

  • Have a runtime error: I/O Error on Activation Code Table I/O =System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command

    I have an outer loop to get records from a database on one server then when it finds records it needs to go to another server and read related records.   I have the execute reader that it's faulting on after it reads the first table gets a id number then builds the command with id and executes that reader based on that command.

    What am I forgetting?

    Thanks!!

    Friday, August 30, 2013 5:10 AM

Answers

  • Hello kindnesshelps,

    Thank you for posting your question to this forum.

    With your description, you want to know why it throws an exception when you use reader.

    If I have misunderstood, please let me know.

    As far as I know the error message may have tell us where the problem occurs.

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

    So it maybe that the reader is not closed before calling anther command.

    If we write code like below:

    string connectionString = "server=(localdb)\\v11.0;database=TestDataBase;Integrated Security=True;";
                    SqlConnection connection = new SqlConnection(connectionString);
                    connection.Open();
                    SqlCommand command1 = new SqlCommand("select * from course", connection);
                    SqlCommand command2 = new SqlCommand("select * from course", connection);
                    SqlDataReader reader = command1.ExecuteReader();
                    reader = command2.ExecuteReader();
                    reader.Close();
                    connection.Close();
                    connection.Dispose();

    It will throw the same exception.

    We should change it to be:

    // change the connect string for your uid/pwd/tns alias
                    string connectionString = "server=(localdb)\\v11.0;database=TestDataBase;Integrated Security=True;";
                    SqlConnection connection = new SqlConnection(connectionString);
                    connection.Open();
                    SqlCommand command1 = new SqlCommand("select * from course", connection);
                    SqlCommand command2 = new SqlCommand("select * from course", connection);
                    SqlDataReader reader = command1.ExecuteReader();
                    reader.Close();
                    reader = command2.ExecuteReader();
                    reader.Close();
                    connection.Close();
                    connection.Dispose();

    We should close the reader object before calling anther command.

    If this does not work for you, could you please share your code so that we can do a test.

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 2, 2013 2:04 AM
    Moderator

All replies

  • I can't even guess about what you're forgetting without seeing a little bit of code ... could you post the relative bits?

    ~~Bonnie Berent DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, August 31, 2013 12:55 AM
  • Example of the code might be helpful to solve the problem, but based on the exception message I think you have one command instance and you try to execute another data reader while reading results from first one.

    Saturday, August 31, 2013 8:41 AM
  • Hello kindnesshelps,

    Thank you for posting your question to this forum.

    With your description, you want to know why it throws an exception when you use reader.

    If I have misunderstood, please let me know.

    As far as I know the error message may have tell us where the problem occurs.

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

    So it maybe that the reader is not closed before calling anther command.

    If we write code like below:

    string connectionString = "server=(localdb)\\v11.0;database=TestDataBase;Integrated Security=True;";
                    SqlConnection connection = new SqlConnection(connectionString);
                    connection.Open();
                    SqlCommand command1 = new SqlCommand("select * from course", connection);
                    SqlCommand command2 = new SqlCommand("select * from course", connection);
                    SqlDataReader reader = command1.ExecuteReader();
                    reader = command2.ExecuteReader();
                    reader.Close();
                    connection.Close();
                    connection.Dispose();

    It will throw the same exception.

    We should change it to be:

    // change the connect string for your uid/pwd/tns alias
                    string connectionString = "server=(localdb)\\v11.0;database=TestDataBase;Integrated Security=True;";
                    SqlConnection connection = new SqlConnection(connectionString);
                    connection.Open();
                    SqlCommand command1 = new SqlCommand("select * from course", connection);
                    SqlCommand command2 = new SqlCommand("select * from course", connection);
                    SqlDataReader reader = command1.ExecuteReader();
                    reader.Close();
                    reader = command2.ExecuteReader();
                    reader.Close();
                    connection.Close();
                    connection.Dispose();

    We should close the reader object before calling anther command.

    If this does not work for you, could you please share your code so that we can do a test.

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 2, 2013 2:04 AM
    Moderator