Поделиться через


Passing DataTable across web/wcf services

Passing DataTable (without embeded into a dataset) across web services is supported in .Net 2.0 framework.  However we found a bug in this area, which might affect using this feature with strong typed dataTable.  For example, when a web service function wants to return a DataTable to the client, an independent dataTable (a dataTable which is not a part of a dataset) will work, but a dataTable inside a dataSet won't work.  What happens is that the client side couldn't deserialize the dataTable from wire.  The root reason is the DataTable inside a dataset inherits a schema namespace from the dataSet, but an independent datatable has an empty namespace (unless we change it).  Data cannot be changed between two dataTables with different namespace.

The problem is that the instance of the dataTable on the client side is created by framework directly. It is impossible to change the namespace without changing generated strong type dataset code.  To work around this issue, the server side need copy data to an independent dataTable before passing it to the client.

That affects both .Net 2.0 web services and 3.0 Windows communication fundation services.

Comments

  • Anonymous
    September 21, 2008
    PingBack from http://kiranpatils.wordpress.com/2008/09/22/the-underlying-connection-was-closed-the-connection-was-closed-unexpectedly-while-returning-data-table-from-wcf-service/

  • Anonymous
    October 07, 2010
    Hi there this the solution: ' Declare a datatable and dataset Dim table As DataTable = New DataTable Dim DS As DataSet = New DataSet ' Set the Datatable table.Columns.Add("Code", GetType(String)) table.Columns.Add("Name", GetType(String)) table.Columns.Add("Address", GetType(String)) ' Fill the Datatable Dim Row As DataRow Row = table.Rows.Add Row.Item("Code") = "0000" Row.Item("Name") = "Peter Jhons" Row.Item("Address") = "NY" ' Second record Row = table.Rows.Add Row.Item("Code") = "0001" Row.Item("Name") = "Charlie Brown" Row.Item("Address") = "Manhatan" ' Add the table to Datatable DS .Tables.Add(table) ' Set the remotingFormat DS .RemotingFormat = SerializationFormat.Binary ' Return the Table from the DatatSet Return DS .Tables(0)