23

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..

2 Answers 2

46

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'
        }    
});  
8
  • Thanks for your answer Johan Haest. I am sorry I am new to this whole thing. Could you please explain where do I need to use the above option. Thank you soo much
    – EagleFox
    Commented Sep 25, 2012 at 14:51
  • Dunno how it is with MVC3 but wit MVC4 JSON.net will be the default serializer for JSON
    – sra
    Commented Sep 25, 2012 at 14:52
  • Also, it says JsonConvert does not exist.... I have using System.Web.Script.Serialization;
    – EagleFox
    Commented Sep 25, 2012 at 14:56
  • Jackson, you'll need to download the json.net dll from their website and add it as a reference to your project. Then you'll be able to use it. You would place the line where you do return Json(myData ... Commented Sep 25, 2012 at 14:59
  • Thanks Johan... did that and now the error is gone, my json format looks like this now... Survey Completed!"{\"myTable\":[{\"Q1\":\"1\",\"Q2\":\"1\",\"Q3\":\"1\",\"Q4\":\"1\",\"Improvements\":\"\",\"Comments\":\"1\"},{\"Q1\":\"1\",\"Q2\":\"2\",\"Q3\":\"3\",\"Q4\":\"4\",\"Improvements\":\"Iphone5\",\"Comments\":\"Iphone14\"},{\"Q1\":\"1\",\"Q2\":\"1\",\"Q3\":\"3\",\"Q4\":\"3\",\"Improvements\":\"This is Comment1-3\",\"Comments\":\"This is Comment2-3\"}]}"... but still can't get it on my gridview
    – EagleFox
    Commented Sep 25, 2012 at 15:24
5

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.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.