In a web forms project, I am loading a jqGrid using a SQL stored procedure where I return the data as json. During initial setup and testing, I was returning 85 rows of data. I then changed the parameters which caused 1,868 rows to be returned, except it was not displaying in the grid.
Upon debugging in Firebug, I saw the error "The length of the string exceeds the value set on the maxJsonLength property". I fixed it by setting the maxJsonLength="2147483647" in my webconfig as found in a popular Stackovrflow post.
So my question is what was the string that caused the error? Is it the length of the whole data record, or the length of the data in one of the columns returned?
I've seen examples of the jqGrid returning much more data. Thanks for any insight.
Update
I took Olegs advice and used Nuget to install Newtonsoft.Json in my project. I then made changes to my code to use it:
In the codebehind - .cs I have this:
using Newtonsoft.Json;
public partial class Default2 : System.Web.UI.Page {
[WebMethod]
public static string GetDataFromDB()
{
DataSet ds = new DataSet();
string con = System.Configuration.ConfigurationManager.ConnectionStrings["SQLCon"].ToString();
SqlConnection SCon = new SqlConnection(con);
SCon.Open();
SqlCommand sqlCmd = new SqlCommand("dbo.usp_GetProjectDetails", SCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add("@ProjNum", SqlDbType.Int).Value = DBNull.Value;
sqlCmd.Parameters.Add("@TrakIt", SqlDbType.VarChar, 255).Value = DBNull.Value;
sqlCmd.Parameters.Add("@Title", SqlDbType.VarChar, 255).Value = DBNull.Value;
sqlCmd.Parameters.Add("@Status", SqlDbType.VarChar, 255).Value = DBNull.Value;
sqlCmd.Parameters.Add("@Dept", SqlDbType.Int).Value = DBNull.Value;
sqlCmd.Parameters.Add("@AssignTo", SqlDbType.Int).Value = DBNull.Value; //19;
sqlCmd.Parameters.Add("@RecDate", SqlDbType.DateTime).Value = DBNull.Value;
sqlCmd.Parameters.Add("@CmpDate", SqlDbType.DateTime).Value = DBNull.Value;
sqlCmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = DBNull.Value;
sqlCmd.Parameters.Add("@ExComp", SqlDbType.Int).Value = DBNull.Value;
sqlCmd.Parameters.Add("@ExAcReq", SqlDbType.Int).Value = DBNull.Value;
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
da.Fill(ds);
SCon.Close();
return JsonConvert.SerializeObject(ds.Tables[0]);
}
The function in .aspx looks like this:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json",
data: "{}",
url: "Default2.aspx/GetDataFromDB",
dataType: "json",
success: function (data) {
data = data.d;
$("#list1").jqGrid({
datatype: "local",
colNames: ["Project #", "Trak-It #", "Priority", "Title", "Status", "Department", "Assigned To", "Resource", "Requestor"],
colModel: [
{ name: 'Project Number', index: 'Project Number', width: 80, key: true, formatter: 'showlink', formatoptions: { baseLinkUrl: 'Details.aspx', target: '_new' } },
{ name: 'Trak-It #', index: 'Trak-It #', width: 80 },
{ name: 'Priority', index: 'Priority', width: 80 },
{ name: 'Title', index: 'Title', width: 200 },
{ name: 'Status', index: 'Status', width: 80 },
{ name: 'Department', index: 'Department', width: 180 },
{ name: 'Assigned To', index: 'Assigned To', width: 100 },
{ name: 'Resource', index: 'Resource', width: 160 },
{ name: 'Requestor', index: 'Requestor', width: 140 }
],
data: JSON.parse(data),
rowNum: 8,
rowList: [10, 20, 30],
pager: '#pager1',
caption: "Test Grid",
viewrecords: true,
ignoreCase: true,
async: true,
loadonce: true,
gridview: true,
width: 1000
});
}
});
});
</script>
And finally in Web.config, I commented out the maxjsonLength:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647">
</jsonSerialization>
</webServices>
</scripting>
But I still get the error = "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."
If I uncomment the web.config settings, it works just fine. If I leave it commented out and bring back less data, it works fine. What am I missing?