c# - Error in Json Serialization "There is already an open DataReader associated with this Command" -
json serialization command giving error. used newtonsoft.json
avoid cyclic reference error on serialization.
private iqueryable<study> getstudiesdata() { var curruser = usermanager.findbyid(user.identity.getuserid()); var curruserrole = curruser.roles.first(); iqueryable<study> studies; if (user.isinrole("superadmin")) { studies = db.studies; //all studies centers } else { var assignedstudies = db.studies.where(s => s.assigneduserid == curruser.id); studies = db.studies.where(s => s.user.centerid == curruser.centerid && s.roleid == curruserrole.roleid) .concat(assignedstudies); } return studies; } //ajax call function gives error public actionresult getstudies(int pagesize = 10, int pagenum = 1) { var studies = getstudiesdata(); var studiescount = studies.count(); var studiespaged = studies.orderby(s=>s.patientid).skip(pagesize*pagenum).take(pagesize); var result = new { totalrows = studiescount, rows = studiespaged }; //this line gives error //there open datareader associated command must closed first. var data = newtonsoft.json.jsonconvert.serializeobject(result, newtonsoft.json.formatting.none, new newtonsoft.json.jsonserializersettings() { referenceloophandling = newtonsoft.json.referenceloophandling.ignore }); return json(data, jsonrequestbehavior.allowget); }
i tried result = studies
, result = studiespaged
, result = studiescount
in var data = newtonsoft.json.jsonconvert.serializeobject(result, ...)
. studiespaged
giving error.
i can solve using multipleactiveresultsets=true;
doing second query?
update
public actionresult getstudies(int pagesize = 10, int pagenum = 1) { var studies = getstudiesdata().tolist(); //solved, created issue //other code }
now json returned looks json again serialized string.
"{\"totalrows\":5,\"rows\":[{\"assigneduser\":null, ..... }]}"
when parsed javascript splitted array of characters.
update2
changed
//data json string after serialization return json(data, jsonrequestbehavior.allowget); //serializing json
to
return content(data, "application/json");
assigning list, , querying should prevent this:
public actionresult getstudies(int pagesize = 10, int pagenum = 1) { var studies = getstudiesdata().tolist(); var studiescount = studies.count(); var studiespaged = studies.orderby(s=>s.patientid).skip(pagesize*pagenum).take(pagesize); var result = new { totalrows = studiescount, rows = studiespaged }; //this line gives error //there open datareader associated command must closed first. var data = newtonsoft.json.jsonconvert.serializeobject(result, newtonsoft.json.formatting.none, new newtonsoft.json.jsonserializersettings() { referenceloophandling = newtonsoft.json.referenceloophandling.ignore }); return json(data, jsonrequestbehavior.allowget); }
Comments
Post a Comment