0

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.

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
annoirq
  • 825
  • 5
  • 18
  • 30
  • Take a look at [this similar question](http://stackoverflow.com/questions/6062192/there-is-already-an-open-datareader-associated-with-this-command-which-must-be-c) – Daniel J.G. Aug 06 '14 at 11:08
  • You have to enable `MARS` as a workaround. But it is more likely your code is not good. – Lukas G Aug 06 '14 at 11:28

2 Answers2

1

Solution is given by Daniel J.G.

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

Everything works, thanks.

Community
  • 1
  • 1
annoirq
  • 825
  • 5
  • 18
  • 30
0

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();
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69