dinesh
dinesh

Reputation: 39

LINQ To SQL error "There is already an open DataReader associated with this Command"

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?

Upvotes: 1

Views: 1930

Answers (4)

philani-allan mbeje
philani-allan mbeje

Reputation: 1

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.

Upvotes: 0

Mathias F
Mathias F

Reputation: 15891

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.

Upvotes: 1

p.campbell
p.campbell

Reputation: 100547

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);
    }
} 

Upvotes: 5

Aristos
Aristos

Reputation: 66641

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

Upvotes: 0

Related Questions