1. #1
    Sencha User
    Join Date
    Jan 2008
    Posts
    33
    Vote Rating
    0
    nshah83 is on a distinguished road

      0  

    Default C# and Ajax.JSON.DefaultConverter

    C# and Ajax.JSON.DefaultConverter


    I am using C# and AJAX.JSON to get the below functionality to work..

    Server code-
    [Ajax.AjaxMethod()]
    public object ServerSideInitialize()
    {
    ArrayList temp = new ArrayList();
    string retVal = null;
    try
    {
    string query = "SELECT * FROM Contact";
    OdbcDataAdapter myDataAdapter = new OdbcDataAdapter(query, this.conn);
    DataSet myDataset = new DataSet();
    myDataset.Clear();
    myDataAdapter.Fill(myDataset, "results");
    //Store the dataset into an arraylist
    foreach (DataRow myRow in myDataset.Tables["results"].Rows)
    {
    temp.Add(myRow);
    }

    StringBuilder sb = new StringBuilder();
    Ajax.JSON.DefaultConverter.ToJSON(ref sb, temp);
    retVal = "{'totalCount':" + myDataset.Tables["results"].Rows.Count + ",'results':" + sb + "}";

    }
    catch (OdbcException ex)
    {
    string text = ex.Message;
    }
    finally
    {
    //Close connection to database
    DB.CloseConnection(this.conn);
    }

    return retVal;
    }

    Client code -
    function ServerSideInitialize_CallBack(response)
    {
    if (response.error != null){
    alert(response.error);
    return;
    }

    //Data Model
    var results = response.value;
    //alert(results)

    var contactFields = Ext.data.Record.create([
    {name:"ID",type:"string"},
    {name:"Name",type:"string"},
    {name:"Occupation",type:"string"}
    ]);

    var ds = new Ext.data.JsonStore({
    data:results,
    root:'rows',
    fields:contactFields
    });

    //Column Model
    var colModel = new Ext.grid.ColumnModel([
    {id:'ID',
    header: "ID",
    width: 60,
    sortable: true,
    dataIndex: 'ID'
    },
    {
    header: "Name",
    width: 150,
    sortable: true,
    dataIndex: 'Name'
    },
    {
    header: "Occupation",
    width: 100,
    sortable: true,
    dataIndex: 'Occupation'
    }
    ]);

    // Build Grid
    var grid = new Ext.grid.EditorGridPanel({
    store: ds,
    cm: colModel,
    stripeRows: true,
    autoExpandColumn: 'ID',
    width:800,
    height:600,
    frame:true,
    title:'Contact Grid'
    });
    grid.render('contactGrid');

    //Grid Events
    grid.on("afteredit", function(e){
    alert(e.record.get('ID'));
    alert(e.field)
    alert(e.value)
    });
    ds.load()
    }

    The value from server displays fine but the information is not displayed.

    Any suggestions?

  2. #2
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    96
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Add a loadexception handler to the store to see if the data is correct, e.g.

    Code:
    listeners: {
      loadexception: function(proxy, store, response, e) {
        alert(e.message);
      }
    }

  3. #3
    Sencha User
    Join Date
    Jan 2008
    Posts
    33
    Vote Rating
    0
    nshah83 is on a distinguished road

      0  

    Default


    I added a loadexception handler and it did not alert any message..I am guessing the data is correct. The firebug displays error: "root has no properties".

  4. #4
    Sencha User
    Join Date
    Jan 2008
    Posts
    33
    Vote Rating
    0
    nshah83 is on a distinguished road

      0  

    Default


    Response from Server (firebug):
    '{\'rows\':[{\'ID\':\'1\',\'Name\':\'AL\',\'Occupation\':\'Alabama\'},{\'ID\':\'2\',\'Name\':\'GA\',
    \'Occupation\':\'Georgia\'}]}'

    The rows grid should display...

  5. #5
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    96
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Not really, a correct response would be:

    Code:
    {'rows':[{'ID':'1','Name':'AL','Occupation':'Ala bama'},{'ID':'2','Name':'GA','Occupation':'Georgia'}]}

  6. #6
    Sencha User
    Join Date
    Jan 2008
    Posts
    33
    Vote Rating
    0
    nshah83 is on a distinguished road

      0  

    Default


    Thanks for the reply!
    I had to decode the reponse client-side!
    var results = Ext.util.JSON.decode( response.value )

Thread Participants: 1