Custom Search + JSON + DataTable + Error

  1. #1

    Custom Search + JSON + DataTable + Error



    Hello!
    I am using code like in this example - https://examples1.ext.net/#/Form/Com...Custom_Search/


    ...
    
    
    // 3 columns.
    // example: 'Jane' 'manager' '123gkj2hg312uy'
    DataTable dt = new DataTable();
    dt = AdUsers.GetUsers();
    
    
    if (dt != null)
     // _ERROR_
     context.Response.Write(string.Format("{{totalUsers:{1},'users':{0}}}", Coolite.Ext.Web.JSON.Serialize(dt), dt.Rows.Count));
    ...

    I get error "An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll"
    Can I return DataTable?
  2. #2

    RE: Custom Search + JSON + DataTable + Error

    Thats not surprising. There are some dependencies in order for an object to be JSON serializable. Datatable is a big Msft object.

    Use a lightweight class or struct to frame the record information you want. Do the iterations over the datatable and transform each record into the datastructure


    List<ContactInfo> myContactList = new List<ContactInfo>();
    foreach (DataRow dr in dt.Rows)
    myContactList .add(new ContactInfo(dr));
  3. #3

    RE: Custom Search + JSON + DataTable + Error



    Juls, thanks for advice! I have found Jayrock JSON which well copes with serialization



  4. #4

    RE: Custom Search + JSON + DataTable + Error

    I think Coolite bundles with NewtonSoft.

    I'd be curious to see how big the json string is coming directly from a serialized Datatable?
  5. #5

    RE: Custom Search + JSON + DataTable + Error

    Sounds like a recursion problem. If you could post an example of whatever .GetUsers() returns, we should be able to recreate locally and test.

    Geoffrey McGill
    Founder
  6. #6

    RE: Custom Search + JSON + DataTable + Error

    Juls (6/16/2009) I think Coolite bundles with NewtonSoft.


    I'd be curious to see how big the json string is coming directly from a serialized Datatable?
    I saw that Coolite is delivered with Newtonsoft, but nevertheless did not use. :)

    In my opinion it is not a lot

    Jayrock.Json.Conversion.JsonConvert.ExportToString (DataTable):

    {"columns":["field1","field2"],"rows":[["1","2"],["1","2"],["1","2"], ... , ["1","2"]]}
    Jayrock.Json.Conversion.JsonConvert.ExportToString (DataTable.Rows):

    [{"field1":"1","field2":"2"},{"field1":"1","field2":"2"},{"field1":"1","field2":"2"}, ... ,{"field1":"1","field2":"2"}]
  7. #7

    RE: Custom Search + JSON + DataTable + Error

    Nice. I might start directly serializing Datatables if its that tight. Save a step. Nice to know. Thanks.
  8. #8

    RE: Custom Search + JSON + DataTable + Error

    geoffrey.mcgill (6/16/2009)Sounds like a recursion problem. If you could post an example of whatever .GetUsers() returns, we should be able to recreate locally and test.
    Any DataTable leads to Error. For Example:

    DataRow dr;
    DataTable dt2 = new DataTable("ADUsers");
    dt2.Columns.Add("cn", typeof(string));
    dr = dt2.NewRow();
    dr["cn"] = "1";
    dt2.Rows.Add(dr);
    
    if (dt != null)
    {
        // _ERROR_
        string strJson = JSON.Serialize(dt2.Rows);        // or dt2
    }

Similar Threads

  1. Error Export DataTable to Excel
    By Andyhincapie in forum 1.x Help
    Replies: 4
    Last Post: Jan 10, 2012, 6:40 PM
  2. Replies: 4
    Last Post: Aug 17, 2011, 2:38 PM
  3. Replies: 1
    Last Post: Aug 17, 2011, 1:35 PM
  4. [CLOSED] How to Json.Serialize a DataTable
    By vali1993 in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Mar 28, 2011, 4:21 PM
  5. Custom Search
    By sharif in forum 1.x Help
    Replies: 0
    Last Post: Jul 14, 2009, 4:04 PM

Posting Permissions