A Circular Reference Was Detected While Serializing an Object of Type 'Subsonic.Schema .Databasecolumn'

A circular reference was detected while serializing an object of type 'SubSonic.Schema .DatabaseColumn'.

It seems that there are circular references in your object hierarchy which is not supported by the JSON serializer. Do you need all the columns? You could pick up only the properties you need in the view:

return Json(new 
{
PropertyINeed1 = data.PropertyINeed1,
PropertyINeed2 = data.PropertyINeed2
});

This will make your JSON object lighter and easier to understand. If you have many properties, AutoMapper could be used to automatically map between DTO objects and View objects.

A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'

Here we go for Solution

I modified my code with below set of code and it worked for me

public JsonResult populateData(string application, string columns, string machine, string pages, string startDate, string endDate)
{
ErrorPage _objError = new ErrorPage();
var ErrorResult = _objError.GetErrorData(application, columns, machine, pages, startDate, endDate);


var result = JsonConvert.SerializeObject(ErrorResult.ErrorData, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});

return Json(result, JsonRequestBehavior.AllowGet);
}

We need to serailize the object rather than sending a direct object of Model.

Thanks.

Json A circular reference was detected while serializing an object of type

Try the following code:

return Json(
parents.Select(x => new {
id = x.id,
name = x.name,
children = x.children.Select(y => new {
// Assigment of child fields
})
}));

...or if you only need the parent properties:

return Json(
parents.Select(x => new {
id = x.id,
name = x.name
}));

It is not really the solution for the problem, but it is a common workaround when serializing DTOs...

Circular reference was detected while serializing an object of type

Thank you Ashokkumar M. Prajapati for providing the hint.

Instead of returning Company object from [WebMethod], I have converted the company object to Json string in code behind and returned it.

Here is my WebMethod:

[WebMethod]
public static string GetCompanyData(int cId)
{
Entities db = new Entities();

var companyRecord = (from cmp in db.Companies
where cmp.CompanyId == cId
select cmp).SingleOrDefault();


if (companyRecord == null)
companyRecord = new Company();

string s = string.Empty;
s = JsonConvert.SerializeObject(companyRecord,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return s;

}

Then I updated the success method of getCompanyDetails(cid) to:

    success: function (result) {
res = JSON.parse(result.d);
$jq14('#cphMainLeft_txtAddress').val(res['CompanyAddress']);
$jq14('#cphMainLeft_txtCountry').val(res['CompanyCountry']);
$jq14('#cphMainLeft_txtCity').val(res['CompanyCity']);
$jq14('#cphMainLeft_txtNewCompanyName').val(res['CompanyName']);
$jq14('#cphMainLeft_txtEmail').val(res['CompanyEmail']);
$jq14('#cphMainLeft_txtPanNo').val(res['CompanyPAN']);
$jq14('#cphMainLeft_txtTinNo').val(res['CompanyTIN']);
$jq14('#cphMainLeft_txtPhone').val(res['CompanyPhone']);
$jq14('#cphMainLeft_txtPincode').val(res['CompanyPincode']);
$jq14('#cphMainLeft_hfTxtCountry').val(res['CompanyCountry']);
$jq14("#cphMainLeft_ddlCompanyType").val(res['CompanyType']);
}

and it worked wonderfully. Thanks again.

A circular reference was detected while serializing an object of type in asp.net and sql

The circular reference is caused by the Json serialization.

See my post here: Preventing StackOverflowException while serializing EF object graph into Json

This post might also help you: EntityFramework to Json workaround?



Related Topics



Leave a reply



Submit