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

Hey everyone,

I ran into the following error this morning:

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

It turns out there are a few causes for this. My issue was that I was attempting to open a result set while already iterating over another one.


 //Retrieve list of cart products and create list of suborders
 var cartProducts = db.CartProducts.Where(cartProduct => cartProduct.CartId == userOrder.CartId);

//Loop through each cart product
foreach(var cartProduct in cartProducts)
{
     //Retrieve suborder
     SubOrder subOrder = subOrders.Find(x => x.BusinessId == cartProduct.Product.BusinessId);

The solution to this was pretty easy thankfully. Simply add “ToList()” to the end of the initial request:


 //Retrieve list of cart products and create list of suborders
 var cartProducts = db.CartProducts.Where(cartProduct => cartProduct.CartId == userOrder.CartId).ToList();

An alternative solution is to modify your connection string in order to allow multiple result multiple result sets. Simply add the following to provider part:

MultipleActiveResultSets=true

Checkout these StackOverflow posts for more info:
http://stackoverflow.com/a/10498776/522859
http://stackoverflow.com/a/6064422/522859

Suddenly Getting The EntityFramework package is not installed on project ”. – ASP.NET MVC

Hey everyone,

I am working on a small project done in ASP.NET MVC4 and started getting the following error:

The EntityFramework package is not installed on project ”.

Get-Package : Cannot validate argument on parameter ‘ProjectName’. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:SitesLearnerLessonspackagesEntityFramework.5.0.0toolsEntityFramework.psm1:611 char:40
+ $package = Get-Package -ProjectName <<<< $project.FullName | ?{ $_.Id -eq 'EntityFramework' }
+ CategoryInfo : InvalidData: (:) [Get-Package], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,NuGet.PowerShell.Commands.GetPackageCommand

The confusing thing was that I’d used it no more than an hour ago. The following StackOverflow post reveals the issue (and the fact that I should’ve read the error more carefully):

http://stackoverflow.com/a/14708897/522859

All I needed to do was select the correct project from the “Default Project” dropdown in the Package Manager Console:

The EntityFramework package is not installed on project ''
The EntityFramework package is not installed on project ”