2

I am returning this from server to JSON, a jquery data table but it returns error:

System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'.
   at WebServices.himher.getUsers1(Int32 iDisplayLength, Int32 iDisplayStart, Int32 iSortCol_0, String sSortDir_0, String sSearch)

.cs code:

public void getUsers1(int iDisplayLength, int iDisplayStart, int iSortCol_0, string sSortDir_0, string sSearch)
{
    try
    {
        basicoperation bop = new basicoperation();
        DataTable dt;

        dt = bop.getUsers(iDisplayLength, iDisplayStart, iSortCol_0, sSortDir_0, sSearch); // fetching users

        dt.TableName = "usersDT1";

        //int iTotalRecords=0;
        //int iTotalDisplayRecords= 0;

        var retObj = new
        {
            iTotalRecords= 20,
            iTotalDisplayRecords= 10,
            aaData= dt
        };

        //string json = JsonConvert.SerializeObject(dt);

        JavaScriptSerializer js = new JavaScriptSerializer();

        Context.Response.Write(js.Serialize(retObj));
    }
    catch (Exception ex)
    {

        throw ex;
    }
}
1
  • The exception message is pretty clear. You have a circular reference in your data models which cannot be serialized. Make a DTO an select only data that should reach the view, not the whole data table Commented Aug 9, 2017 at 6:21

3 Answers 3

5

The problem is that your retObj contains a DataTable, this cannot be serialised to JSON as is, as it has circular references inside it.

This article shows different ways to serialise the datatable;

http://www.c-sharpcorner.com/UploadFile/9bff34/3-ways-to-convert-datatable-to-json-string-in-Asp-Net-C-Sharp/

My preferred is probably the last way

using Newtonsoft.JSON;  

public string DataTableToJSONWithJSONNet(DataTable table) {  
   string JSONString=string.Empty;  
   JSONString = JSONConvert.SerializeObject(table);  
   return JSONString;  
}  
Sign up to request clarification or add additional context in comments.

Comments

0

It's pretty clear you have a circular reference here in your code aaData= dt.
You can use below code to solve this issue,

JsonConvert.SerializeObject(retObj, Formatting.None, 
                    new JsonSerializerSettings {ReferenceLoopHandling = 
                                          ReferenceLoopHandling.Ignore});

Comments

0

An easier alternative to solve this problem is to return an string, and format that string to json with JavaScriptSerializer.

public string GetEntityInJson()
{
   JavaScriptSerializer j = new JavaScriptSerializer();
   var entityList = dataContext.Entitites.Select(x => new { ID = x.ID, AnotherAttribute = x.AnotherAttribute });
   return j.Serialize(entityList );
}

It is important the "Select" part, which choose the properties you want in your view. Some object have a reference for the parent. If you do not choose the attributes, the circular reference may appear, if you just take the tables as a whole.

Do not do this:

public string GetEntityInJson()
{
   JavaScriptSerializer j = new JavaScriptSerializer();
   var entityList = dataContext.Entitites.toList();
   return j.Serialize(entityList );
}

Do this instead if you don't want the whole table:

public string GetEntityInJson()
{
   JavaScriptSerializer j = new JavaScriptSerializer();
   var entityList = dataContext.Entitites.Select(x => new { ID = x.ID, AnotherAttribute = x.AnotherAttribute });
   return j.Serialize(entityList );
}

This helps render a view with less data, just with the attributes you need, and makes your web run faster.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.