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
  • 95,348
  • 63
  • 249
  • 319
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
  • 95,348
  • 63
  • 249
  • 319
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,202
  • 20
  • 87
  • 155
0

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

Aristos
  • 64,863
  • 15
  • 114
  • 148
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
  • 31,610
  • 8
  • 53
  • 74