annoirq
annoirq

Reputation: 865

There is already an open DataReader associated with this Command which must be closed first (ASP.NET MVC4)

I have some code in loop:

foreach (var report in reports)
{
    var Current_Project = db.Projects.Where(c_p => c_p.project_name == report.pcode).FirstOrDefault();
}

When I run it, it shows message error:

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

Even when I put

var Current_Project = db.Projects.FirstOrDefault();

When I try to put it outside of the loop, it works.

Please, advice.

Upvotes: 0

Views: 171

Answers (2)

annoirq
annoirq

Reputation: 865

Solution is given by Daniel J.G.

"Add MultipleActiveResultSets=true to the provider part of your connection string"

Everything works, thanks.

Upvotes: 1

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

somewhere in your application you are using datareader and you have not close it that is why error is coming,connection with database is still open that is why error is coming.

Use this to close datareader:

datareader.Close();

Upvotes: 0

Related Questions