maxJsonLength error when serializing data in .net 2.0

594 Views Asked by At

I am getting the following error when serializing data in asp.net 2.0. Large data is being sent from the database but it is less than the max limit because it works in .Net 4.5 when setting the MaxJsonLength in the web.config file.

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

At this point, the project can not be upgraded to higher .net version.

The following maxJsonLength doesn't work in the web config file, i get an error. I could be due to .net version in IIS. It works in 4.5 though.

  <system.web.extensions>
  <scripting>
  <webServices>
    <jsonSerialization maxJsonLength="819200000" />
  </webServices>
  </scripting>
  </system.web.extensions>

Q1. Is there any way to set the Max limit in .net 2.0 web config file?

Q2. Why i am getting an error message when the max limit is set in the server side code?

Here is the code to serialize dataset using JavaScriptSerializer

private string GetJSON(DataSet ds)
{

Int32 len = 2147483644;

System.Web.Script.Serialization.JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength = len };

ArrayList root = new ArrayList();

List<Dictionary<string, object>> table;

Dictionary<string, object> data;

foreach (DataTable dt in ds.Tables)
{
    table = new List<Dictionary<string, object>>();
    foreach (DataRow dr in dt.Rows)
    {
        data = new Dictionary<string, object>();
        foreach (DataColumn col in dt.Columns)
            data.Add(col.ColumnName, dr(col));
            table.Add(data);
    }
    root.Add(table);
}
return serializer.Serialize(root);

}

I have been to the following site which has got some good info but it doesn't resolve the .net 2.0 issue;

http://rion.io/2013/04/28/handling-larger-json-string-values-in-net-and-avoiding-exceptions/

0

There are 0 best solutions below