This question is a part of my original post here Get Data Into Extjs GridPanel
Below is my Controller that reads data from sql db and then I am trying to encode the result as JSON and send the data back to my gridview.js
public JsonResult writeRecord() //public string writeRecord() { Response.Write("Survey Completed!"); SqlConnection conn = DBTools.GetDBConnection("ApplicationServices2"); string sqlquery = "SELECT Q1, Q2, Q3, Q4, Improvements, Comments FROM myTable"; SqlDataAdapter cmd = new SqlDataAdapter(sqlquery, conn); DataSet myData = new DataSet(); cmd.Fill(myData, "myTable"); conn.Open(); conn.Close(); return Json(myData, JsonRequestBehavior.AllowGet); //return myData.GetXml(); }
Here lies the problem, with the above code, I get my gridview table with no data when executing gridview.js, but if I directly access my controller's method like this
http://localhost:55099/GridView/writeRecord
I get this error,
A circular reference was detected while serializing an object of type 'System.Globalization.CultureInfo'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Globalization.CultureInfo'.
Can someone please help..
The "Converting circular structure to JSON" error occurs when we pass an object that contains circular references to the JSON. stringify() method. To solve the error, make sure to remove any circular references before converting the object to JSON.
A circular structure is an object that references itself. To be able to stringify such objects, developers can utilize the replacer parameter in the stringify() method, making sure the function that is being passed in, filters out repeated or circular data.
The circular reference error message "There are one or more circular references where a formula refers to its own cell either directly or indirectly. This might cause them to calculate incorrectly. Try removing or changing these references, or moving the formulas to different cells."
It will not be a problem for garbage collection: any new Garbage Collector (>IE6) will handle circular references just fine! It might be a problem though if you are doing recursive functions, or printing the object. So the answer is: it is no problem unless you screw up yourselves :-) Show activity on this post.
I use the following tool for serializing and deserializing JSON:
http://james.newtonking.com/pages/json-net.aspx
It's very easy to use and very lightweight.
While serializing we use this option:
JsonConvert.SerializeObject(myObject, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })
It ignores circular references.
Also json.net from newtonking is extremely fast.
The other options is to use DTO's and map them via Automapper as mentioned by Diver.
Edit: I suspect your store is wrong:
var store = Ext.create('Ext.data.JsonStore', { storeId: 'myData', reader: new Ext.data.JsonReader({ root: 'myTable', fields: [{ name: 'Q1', type: 'int' }, { name: 'Q2', type: 'int' }, { name: 'Q3', type: 'int' }, { name: 'Q4', type: 'int' }, { name: 'Q5', type: 'int' }, { name: 'Improvements', type: 'string' }, { name: 'Comments', type: 'string'}] }), proxy: { type: 'json', url: 'GridView/writeRecord' } });
It's because something inside CultureInfo has a reference to itself (this type) and in process of converting to JSON it fails. To avoid this situation, you should use ViewModels (return to client only information that is needed). You can read more here http://blogs.msdn.com/b/dphill/archive/2009/01/31/the-viewmodel-pattern.aspx
In your situation you shoud create ViewModels for your data, convert your data to these data types and return them converted to JSON. For the purpose of converting from Model to ViewModel consider using AutoMapper http://automapper.codeplex.com/ or some similar tool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With