1

There is already an open DataReader associated with this Command

I am getting this error when I retrieve data from the DataContext object.

How can this be fixed?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
dinesh
  • 39
  • 4

4 Answers4

5

Ensure that you're not declaring your DataContext as static. Create and destroy your DataContext on each use.

public MyDataClass{

    CustomerDataContext db;

    public void MyDataClass()
    {
       db = new CustomerDataContext();
    }

    public Customer GetCustomer(int id)
    {
       return db.Customers.SingleOrDefault(c=>c.ID == id);
    }
} 
p.campbell
  • 98,673
  • 67
  • 256
  • 322
1

Check this post

Is mixing ADO.NET and LINQ-TO-SQL bad? My data layer isn't working

It realy depends how you store, access and dispose the datacontext. Try to reproduce the error using a load test tool. I use jmeter. Lots of people don't know that they have this issue, because they have too little traffic.

Community
  • 1
  • 1
Mathias F
  • 15,906
  • 22
  • 89
  • 159
0

You have forget to close the DataReader, and you start one more DataReader on the same connection.

Aristos
  • 66,005
  • 16
  • 114
  • 150
0

What helped me was converting all the IQueryable types associated with the query that is throwing the error to more native types before using them in another query.

adiga
  • 34,372
  • 9
  • 61
  • 83