How to convert the datatable into JSON String
-
Hi I am using Web Methods and Stateful Classes for DML Operations i.e. (Insert, Update and Delete). Now i want to populate/bind my control from the saved data. I am gettting the datatable from the ajax method and can access it through javascript. I have binded controls synchronously with the values returned through datatable. I want to bind data asynchronously, for that I want to convert datatable into JSON String and return that to javascript. Pls help to for the same.
-
Hi I am using Web Methods and Stateful Classes for DML Operations i.e. (Insert, Update and Delete). Now i want to populate/bind my control from the saved data. I am gettting the datatable from the ajax method and can access it through javascript. I have binded controls synchronously with the values returned through datatable. I want to bind data asynchronously, for that I want to convert datatable into JSON String and return that to javascript. Pls help to for the same.
How do you return the data to Javascript ? is it in XML format ? now I think you will have two approaches: First convert the datatable to XML and then to JSON in the code behind and send the JSON presentation back to javascript then bind, the second approach is to convert the datatabe to XML in code behind and then send the XML presentation to Javascript, in Javascript you convert the XML to JSON and then bind. In both approaches the hard part is to convert the XML to JSON either in Javascript or code behind, there are a lot of articles on this subject ... try to search on that and pick the one that suits you.
Sincerely Samer Abu Rabie Note: Please remember to rate this post to help others whom reading it.
-
Hi I am using Web Methods and Stateful Classes for DML Operations i.e. (Insert, Update and Delete). Now i want to populate/bind my control from the saved data. I am gettting the datatable from the ajax method and can access it through javascript. I have binded controls synchronously with the values returned through datatable. I want to bind data asynchronously, for that I want to convert datatable into JSON String and return that to javascript. Pls help to for the same.
You have to loop through the rows to format the data to JSON string. Let's consider an example that your datatable contains two columns: - FirstName - LastName then, the code might be: Note: - Am not giving the exact code
protected function ConvertDataTableToJSON(byval dt as DataTable)
Dim strJSON as string
strJSON = "{"
for each row as DataRow in dt.Rows
strJSON = strJSON + "'firstName':" + "'" + row("FirstName") + "',"
strJSON = strJSON + "'lastName':" + "'" + row("LastName") + "',"
end forstrJSON = strJSON + "}" return strJSON
end function
Hope this will be useful. Please refer the following link for JSON format: http://en.wikipedia.org/wiki/JSON[^] Thanks, Rajdev KR