0

I'm receiving this error

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

Here is the code that it errors on.

var summaryViewModel = new YogaSpaceSummaryViewModel
            {
                SpaceImage = (from u in space.Images orderby u.Ordering ascending select u.ImageThumbnail).First(),
                Title = space.Overview.Title,
                SpaceId = space.YogaSpaceId,
                DateCreated = space.DateCreated.ToShortDateString(),
                StepsToList = space.StepsToList,
                Status = space.Status
            };

which part of this code here

YogaSpaceOverviewViewModel overviewViewModel = new YogaSpaceOverviewViewModel();

        foreach (YogaSpace space in yogaSpaces)
        {
            var summaryViewModel = new YogaSpaceSummaryViewModel
            {
                SpaceImage = (from u in space.Images orderby u.Ordering ascending select u.ImageThumbnail).First(),
                Title = space.Overview.Title,
                SpaceId = space.YogaSpaceId,
                DateCreated = space.DateCreated.ToShortDateString(),
                StepsToList = space.StepsToList,
                Status = space.Status
            };

            overviewViewModel.YogaSpaceSummarys.Add(summaryViewModel);
        }
5
  • As the message suggests, check if any of your previously opened/used DataReaders is properly closed. Commented Nov 4, 2015 at 4:46
  • how would I check that? Commented Nov 4, 2015 at 4:46
  • Post the code for DB connections Commented Nov 4, 2015 at 4:47
  • If the datareader is null. Make sure when the reader is closed you set it to null. It is automatically null initially. Commented Nov 4, 2015 at 4:47
  • I placed a .ToList() after yogaSpaces in the foreach loop and that seemed to fix it. Commented Nov 4, 2015 at 4:51

3 Answers 3

1

You might want to wrap downward using a using so the scope will cause all objects to bedisposed. Also, to circumvent a lzy loading issue with disposed datacontext's you will probally be better served to ToList() ot FirstOrDefault() any result you are returning to ensure the collection has been enumerated and if the context is disposed then everything is dandy.

Sign up to request clarification or add additional context in comments.

Comments

0
 <connectionStrings>
 <add name="DatabaseEntities" connectionString="metadata=res://*/Database.csdl|res://*/Database.ssdl|res://*/Database.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=Server;Initial Catalog=Database;Persist Security Info=True;User ID=sa;Password=passowrd;MultipleActiveResultSets=True;Application Name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
 </connectionStrings>

Enable MultipleActiveResultSets in Webconfig

Comments

0

Add MultipleActiveResultSets=true to the provider part of your connection string (where Data Source, Initial Catalog, etc. are specified).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.