Return Data Table From Web Service

robertx

Member
Joined
Nov 23, 2007
Messages
7
Programming Experience
5-10
With .NET 1.1, it was not possible to return a DataTable from a web service - only a DataSet.

Finally, I have successfully returned a DataTable from a web service (using .NET 2.0) but I had to do the following before I succeeded:

1. I had to set the "TableName" property of the DataTable before returning it.

2. The proxy class (generated in VS) specifies the return type as

[Procedure Name]Response[Procedure Name]Result

For example, if the procedure in the web service is called "GetDT", the return type in the proxy class is "GetDTResponseGetDTResult".

I had to change each instance of this in the proxy class to "System.Data.DataTable". However, when I update the web reference, it just changes back which causes unnecessary work each time I update it.

Can anyone explain this behaviour and how it can be fixed?
 
DataTable is not among the supported data types to pass to/from webservices, why can't you pass a DataSet containing a single DataTable?
 
Hi John,

I am in the process of upgrading a web service that I wrote using using VB.NET 2003. I knew that is was not possible to return a DataTable object using the .NET framework 1.1 so I just returned DataSet objects instead which has worked just fine. I have read some literature that suggests that from the .NET framework 2.0, it is possible to return a DataTable.

See the following:

http://dotnetforum.lk/forums/p/1407/8015.aspx

http://forums.asp.net/p/1185325/2024445.aspx

My rationale is that if returning a DataTable is now possible that it would be slightly tidier and possibly provide better performance so therefore it may be prudent to do so.

The following thread describes the same problem to which I have referred:

http://www.thescripts.com/forum/thread605387.html

The reality is that I was able to return a DataTable object with some tweaking of the VS-generated proxy class. If you can convince me that returning DataTable objects is either not intended to be supported or that it is a disadvantage to do so then I'll just stick to returning DataSet objects. However, I would like to understand what is happening under the hood so that I can develop the best coding practices going forward.
 
The link in Neals post which you missed is to the documentation which says the same for all .Net version 1-3. Convinced?
 
I didn't miss it - in fact I read the whole document.

I am not convinced because my own findings are not consistent with Microsoft's documentation. Whilst that literature does not state that a DataTable object cannot be returned, it does not list DataTable amongst the supported types.
 
Actually on second thought the DataTable was supported from .Net 2.0 since it from then implements IXmlSerializable. I just tested this myself and it worked right out of the box now, return type in proxy class is System.Data.DataTable. The possibility of me misunderstanding that documentation before is very likely. Could your problem be related to you not having VS SP1? Perhaps there was a bug with this before SP1?
 
Actually, I installed VS2005 SP1 just last week. Just now, I tried to delete and recreate the web reference but the same thing happened.

Just out of interest, I ran a query that returns 4,600 records from a web service returning a DataSet and then ran it again returning a DataTable. I used a StopWatch object to time the process which took about 8 seconds in both instance so (not surprisingly) there is no difference in performance.
 
Back
Top