Search code examples
c#asp.net-mvcnopcommerce

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


I'm getting product images from this services getting this error in version 3.8:

var defaultProductPicture=_pictureService.GetPicturesByProductId(productmodel.Id,1).FirstOrDefault();
productmodel.productImagUrl = _pictureService.GetPictureUrl(defaultProductPicture, 75, true);

My GetPicturesByProductId services is:

public virtual IList<Picture> GetPicturesByProductId(int productId, int recordsToReturn = 0)
{
    if (productId == 0)
        return new List<Picture>();


    var query = from p in _pictureRepository.Table
                join pp in _productPictureRepository.Table on p.Id equals pp.PictureId
                orderby pp.DisplayOrder
                where pp.ProductId == productId
                select p;

    if (recordsToReturn > 0)
        query = query.Take(recordsToReturn);

    var pics = query.ToList();
    return pics;
}

I don't know why GetPictureByProductId faces this error in newer version 3.8. In version 3.7 everything working fine.


Solution

  • This can happen if you execute a query while iterating over the results from another query.

    So just add MultipleActiveResultSets=True to the connection string in setting.txt file.

    Hope this helps!