jsonserializing gives error An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

Jan 16, 2009 at 6:11 AM
I am using the northwind sample database, using two tables categories and product, I have generated the EDM for these two tables and then I am trying to use the JsonConverter class to generate a json object,

the code snippet is 

 

protected void FillGvProducts()

 

{

 

int categoryID = 0;

 

 

string xmlstring;

 

 

if (!string.IsNullOrEmpty(ddlCategory.SelectedValue))

 

categoryID =

Convert.ToInt32(ddlCategory.SelectedValue);

 

 

Categories selectedCategory = _categoriesFacade.GetCategoryByID(categoryID);

 

selectedCategory.Picture =

null;

 

 

if (selectedCategory == null) return;

 

selectedCategory.Products.Load();

gvProducts.DataSource = selectedCategory.Products;

gvProducts.DataBind();

 

//xmlstring = serialzeentity(selectedCategory);

 

 

//xmlstring = javascriptserializer(selectedCategory);

 

 

//xmlstring = datacontractserializer(selectedCategory);

 

xmlstring = newtonsoftserializer(selectedCategory);

}

 

private string newtonsoftserializer(Categories selectedCategory)

 

{

 

JsonSerializerSettings js = new JsonSerializerSettings();

 

js.DefaultValueHandling =

DefaultValueHandling.Ignore;

 

js.ReferenceLoopHandling =

ReferenceLoopHandling.Ignore;

 

js.NullValueHandling =

NullValueHandling.Ignore;

 

js.ObjectCreationHandling =

ObjectCreationHandling.Reuse;

 

js.MissingMemberHandling =

MissingMemberHandling.Ignore;

 

 

 

return Newtonsoft.Json.JsonConvert.SerializeObject(selectedCategory, Newtonsoft.Json.Formatting.Indented, js);

 

}


However while serialzing it gives me a stackoverflow error. If I dont lazy load the product table then it works fine, but loading the product table and serializing gives me this error.


Kindly help  me to know where I am going wrong, do I require to change any association settings in the EDMX do avoid cyclical referencing, and will it effect the data usage of the two tables, what is the work around.


Thanks!