-
25 Jan 2008 7:45 AM #1
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?
-
25 Jan 2008 7:47 AM #2Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 41
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); } }
-
25 Jan 2008 8:09 AM #3
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".
-
25 Jan 2008 8:30 AM #4
Response from Server (firebug):
'{\'rows\':[{\'ID\':\'1\',\'Name\':\'AL\',\'Occupation\':\'Alabama\'},{\'ID\':\'2\',\'Name\':\'GA\',
\'Occupation\':\'Georgia\'}]}'
The rows grid should display...
-
25 Jan 2008 12:23 PM #5Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 41
Not really, a correct response would be:
Code:{'rows':[{'ID':'1','Name':'AL','Occupation':'Ala bama'},{'ID':'2','Name':'GA','Occupation':'Georgia'}]}
-
25 Jan 2008 2:26 PM #6
Thanks for the reply!
I had to decode the reponse client-side!
var results = Ext.util.JSON.decode( response.value )


Reply With Quote