Error: “Calling ‘Read’ when datareader is closed…”

1 answer

By dotnetomBest answer

You need to call a method like ToList in you data access layer to force the request to a database:

using (var context = new Admin_TestEntities())
    var data = context.Leave_GetDetails("Recommended").ToList();
    return View(data);

Methods like AsEnumerable() do not make a request to a database. The request is only made when you first access the data (in your case – in a view) or when you explicitly call a method which makes a call to the database (for example ToList()).

You get an error because when you try to access object in your view this is when the call to the database is actually made, but at the same time your object context is already disposed. To fix it you should explicitly force database query using ToList while the context is not yet disposed, that is in your controller method


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s