2

I have the following code:

For some reason or another, it is generating the exception "Invalid attempt to call Read reader is closed". Can someone help me to solve it? I have no idea why it is generating this exception as I am closing the connection after reading the contents of the reader.

It is generating the exception on this line:

                    while (rdr.Read())
Matthew
  • 4,477
  • 21
  • 70
  • 93
  • For some reason or another, this line `results.Load(rdr);` is causing the trouble. – Wiktor Zychla Jun 22 '13 at 17:49
  • It is loading the results in a Data Table. I declared the data table as follows: DataTable results = new DataTable(); – Matthew Jun 22 '13 at 17:50
  • The DataTable has been declared well before it is loaded with the results of the query. It is not being modified in any other way before being loaded with the results of the query. – Matthew Jun 22 '13 at 17:51

1 Answers1

4

I solved it.

The problem was that I was loading the datatable inside the read block. I modified the code as follows and it works now.

if (rdr.HasRows)
{
    results.Load(rdr);
}
לבני מלכה
  • 15,925
  • 2
  • 23
  • 47
Matthew
  • 4,477
  • 21
  • 70
  • 93