asp.net mvc - Error: "Calling 'Read' when datareader is closed...." -
here code view:
@model ienumerable<sample2.models.leave_getdetails_result1> @{ viewbag.title = "index"; } <div> @foreach (var item in model) { <dl class="dl-horizontal"> <dt> @html.displaynamefor(model => model.la_staffcode) </dt> <dd> @html.displayfor(model => item.la_staffcode) </dd> <dd> @html.displayfor(model => item.la_appno) </dd> </dl> } </div> <p> @*@html.actionlink("edit", "edit", new { id = model.staff_code }) |*@ @html.actionlink("back list", "index") </p>
when execute, gives error:
"calling 'read' when datareader closed not valid operation"
edited:
the controller code below calling stored procedure:
using (var context = new admin_testentities()) { var data = context.leave_getdetails("recommended"); return view(data.asenumerable()); }
you need call method tolist
in data access layer force request database:
using (var context = new admin_testentities()) { var data = context.leave_getdetails("recommended").tolist(); return view(data); }
methods asenumerable()
not make request database. request made when first access data (in case - in view) or when explicitly call method makes call database (for example tolist()
).
you error because when try access object in view when call database made, @ same time object context
disposed. fix should explicitly force database query using tolist
while context not yet disposed, in controller method
Comments
Post a Comment