I have a simple web method written in the code-behind (not a separate asmx) of a test aspx page.
All it does is return a DataTable(or DataSet) both are failing, gives a blank 500 error saying "There was an error processing the request"
If I switch the return variable type to String, it works fine
I think my issue has to do with the version of the System.Web.Extensions, is that the assembly that actually contains the JSON communication implementation?
I've read all kinds of tutorials about how it should be possible to return the DataTable to javascript and then read the properties with javascript syntax.
For some reason it isn't working for me.
My web extensions is imported through my web.config with the following version
I also set the ContentType, here is my code (I'm using a normal class, it's variable is called response)
// Setup the web response
context.Response.ContentType = "text/javascript";
// Serialize the object as JSON to the web response
DataContractJsonSerializer serializer = new DataContractJsonSerializer(response.GetType());
serializer.WriteObject(context.Response.OutputStream, response);
Dont forget to click "Mark as answer" on the post that helped you.
================================================
Why catch a fish to feed someone, when you can teach them to fish and they can feed themselves
You won’t be able to send a DataTable using ASP.NET AJAX 1.0, but IMHO you are better sending an array of a complex type instead, a DataTable is a very big and heavy object with lots of properties you won’t use in the client, it’s not really suited to be
used in an AJAX call.
If this post helped you please remember to set it as Answer so it can help others.
Rather than returning Dataset /Datatable to javascript, a better approach will be to convert it on JSON object and then parse resultset in javascript on page.
<WebMethod(False)>
Public Shared Function GetDataFromServer(id As String) As String
Dim objDAL As New DALOBJECT
Dim dt As New DataTable("MyTable")
dt = objDAL.GetData(id)
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
Return serializer.Serialize(rows)
End Function
-------------
On page in javascript function use following to parse result-
n4esa
0 Points
22 Posts
Returning DataSet/Datatable from Webmethod to Javascript
Sep 16, 2010 12:24 AM|LINK
I have a simple web method written in the code-behind (not a separate asmx) of a test aspx page.
All it does is return a DataTable(or DataSet) both are failing, gives a blank 500 error saying "There was an error processing the request"
If I switch the return variable type to String, it works fine
I think my issue has to do with the version of the System.Web.Extensions, is that the assembly that actually contains the JSON communication implementation?
I've read all kinds of tutorials about how it should be possible to return the DataTable to javascript and then read the properties with javascript syntax.
For some reason it isn't working for me.
My web extensions is imported through my web.config with the following version
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
andrewjboyd
Contributor
4426 Points
864 Posts
Re: Returning DataSet/Datatable from Webmethod to Javascript
Sep 16, 2010 05:09 AM|LINK
My app that returns JSON uses
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
I also set the ContentType, here is my code (I'm using a normal class, it's variable is called response)
// Setup the web response context.Response.ContentType = "text/javascript"; // Serialize the object as JSON to the web response DataContractJsonSerializer serializer = new DataContractJsonSerializer(response.GetType()); serializer.WriteObject(context.Response.OutputStream, response);================================================
Why catch a fish to feed someone, when you can teach them to fish and they can feed themselves
karthicks
All-Star
32144 Points
5529 Posts
Re: Returning DataSet/Datatable from Webmethod to Javascript
Sep 16, 2010 07:01 AM|LINK
hi,
if you are using Asp.Net AJAX then you can use PageMethods
http://weblogs.asp.net/infinitiesloop/archive/2006/12/14/ajax-futures-december-ctp-returning-datasets-datatables-and-datarows-from-a-webservice-or-pagemethod.aspx
http://dotnetslackers.com/Ajax/re-23169_Quick_Reference_for_the_Atlas_DataTable.aspx
Karthick S
n4esa
0 Points
22 Posts
Re: Returning DataSet/Datatable from Webmethod to Javascript
Sep 16, 2010 06:14 PM|LINK
Ok, I can't use ASP.NET 3.5 for the solution I'm stuck with 2.0
Can I use strictly the web.extensions version of 3.5 for the purpose of Ajax and leave everything else the same?
I am using the PageMethod pattern mentioned, it isn't working, hence my purpose for posting.
Song-Tian - ...
All-Star
43715 Points
4304 Posts
Microsoft
Re: Returning DataSet/Datatable from Webmethod to Javascript
Sep 20, 2010 06:30 AM|LINK
Hi,
Why you not upgrate your application to 3.5? If you still want to use 2.0, you could have try.
Feedback to us
Develop and promote your apps in Windows Store
mrmercury
Star
8760 Points
1291 Posts
Re: Returning DataSet/Datatable from Webmethod to Javascript
Sep 20, 2010 02:57 PM|LINK
You won’t be able to send a DataTable using ASP.NET AJAX 1.0, but IMHO you are better sending an array of a complex type instead, a DataTable is a very big and heavy object with lots of properties you won’t use in the client, it’s not really suited to be used in an AJAX call.
saurabhmaury...
Member
2 Points
2 Posts
Re: Returning DataSet/Datatable from Webmethod to Javascript
Jun 27, 2012 09:13 AM|LINK
Rather than returning Dataset /Datatable to javascript, a better approach will be to convert it on JSON object and then parse resultset in javascript on page.
<WebMethod(False)>
Public Shared Function GetDataFromServer(id As String) As String
Dim objDAL As New DALOBJECT
Dim dt As New DataTable("MyTable")
dt = objDAL.GetData(id)
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
Return serializer.Serialize(rows)
End Function
-------------
On page in javascript function use following to parse result-
var jsResult=JSON.parse(result);
chetan.sarod...
All-Star
66589 Points
11270 Posts
Re: Returning DataSet/Datatable from Webmethod to Javascript
Jun 28, 2012 03:48 AM|LINK
Refer this
http://forums.asp.net/t/1368554.aspx/1
http://chanmingman.wordpress.com/2007/10/21/returning-dataset-from-webmethod-in-web-services/
Senior Software Engineer,
Approva Systems Pvt Ltd, Pune, India.