Microsoft KB Archive/306134

= Problems using an XML Web service that returns a DataTable =

Article ID: 306134

Article Last Modified on 10/25/2004

-

APPLIES TO


 * Microsoft Web Services Enhancements for Microsoft .NET 2.0
 * Microsoft Web Services Enhancements for Microsoft .NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft ASP.NET 1.0
 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft ASP.NET 1.1

-



This article was previously published under Q306134



SYMPTOMS
When you browse to the URL of an XML Web service that returns a DataTable from one of its Web methods, you may receive the following error message:

System.Data.DataRelation cannot be serialized because it does not have a default public constructor.

You may also see an error message similar to the following when you try to set a Web reference to this XML Web service in the Visual Studio .NET integrated development environment (IDE):

Internal Server Error. Unable to request &quot;http://localhost/Webservice1/Service1.asmx?WSDL&quot;. The server responded with error code &quot;ProtocolError&quot;.

With the .NET Framework 1.1 Service Pack 1 (SP1), you may receive the following error message:

System.NotSupportedException: Cannot serialize member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite because it is an interface.

You may also receive a blank page instead of any of these error messages if Show friendly HTTP error messages is enabled in Microsoft Internet Explorer. By default, the Show friendly HTTP error messages option is enabled.

If you try to add a Web reference to an XML Web service that returns a DataTable, you may receive the following error message:

The document at the url http:// /vdir/service1.asmx was not recognized as a known document type.



CAUSE
The DataTable, DataRow, DataView, and DataViewManager objects cannot be serialized and cannot be returned from an XML Web service. To return less than a complete DataSet, you must copy the data that you want to return to a new DataSet.



RESOLUTION
To resolve this issue, return a DataSet instead of a DataTable. DataSet objects can contain one or more DataTable objects.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
NOTE: Sample code is provided for Visual Basic .NET, Visual C# .NET, and Visual J# .NET.   Add a Web method that returns a DataTable to an existing XML Web service. The following code creates a connection to a Microsoft SQL Server database and retrieves the Authors table. If you want to use the code, modify it to connect to one of your SQL Server computers. 'Visual Basic  Function GiveMeADataTable As System.Data.DataTable Dim conn As New System.Data.SqlClient.SqlConnection(&quot;Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;&quot;) Dim ds As New System.Data.DataSet Dim adapter As New System.Data.SqlClient.SqlDataAdapter adapter.SelectCommand = New System.Data.SqlClient.SqlCommand(&quot;Select * From Authors&quot;, conn) adapter.Fill(ds, &quot;Authors&quot;) Return ds.Tables(&quot;Authors&quot;) End Function //Visual C#  [WebMethod] public System.Data.DataTable GiveMeADataTable {       System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(&quot;Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;&quot;); System.Data.DataSet ds = new System.Data.DataSet; System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter; adapter.SelectCommand = new System.Data.SqlClient.SqlCommand(&quot;Select * From Authors&quot;, conn); adapter.Fill(ds, &quot;Authors&quot;); return ds.Tables[&quot;Authors&quot;]; }

/**Visual J# */ /** @attribute WebMethod */ public System.Data.DataTable GiveMeADataTable {       System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(&quot;Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;&quot;); System.Data.DataSet ds = new System.Data.DataSet; System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter; adapter.set_SelectCommand(new System.Data.SqlClient.SqlCommand(&quot;Select * From Authors&quot;, conn)); adapter.Fill(ds, &quot;Authors&quot;); System.Data.DataTableCollection dtc = ds.get_Tables; return dtc.get_Item(&quot;Authors&quot;); }                     Compile the XML Web service. Browse to the URL of the XML Web service in which you just added the code. Note that you receive an error message.  Modify the code as follows to return a DataSet instead of a DataTable: 'Visual Basic  Function GiveMeADataSet As System.Data.DataSet Dim conn As New System.Data.SqlClient.SqlConnection(&quot;Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;&quot;) Dim ds As New System.Data.DataSet Dim adapter As New System.Data.SqlClient.SqlDataAdapter adapter.SelectCommand = New System.Data.SqlClient.SqlCommand(&quot;Select * From Authors&quot;, conn) adapter.Fill(ds, &quot;Authors&quot;) Return ds   End Function //Visual C#  [WebMethod] public System.Data.DataSet GiveMeADataSet {       System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(&quot;Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;&quot;); System.Data.DataSet ds = new System.Data.DataSet; System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter; adapter.SelectCommand = new System.Data.SqlClient.SqlCommand(&quot;Select * From Authors&quot;, conn); adapter.Fill(ds, &quot;Authors&quot;); return ds; }

/**Visual J# */ /** @attribute WebMethod */ public System.Data.DataSet GiveMeADataSet {       System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(&quot;Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;&quot;); System.Data.DataSet ds = new System.Data.DataSet; System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter; adapter.set_SelectCommand(new System.Data.SqlClient.SqlCommand(&quot;Select * From Authors&quot;, conn)); adapter.Fill(ds, &quot;Authors&quot;); return ds; }                   </li> Compile the XML Web service.</li> Browse to the URL of the XML Web service in which you just added the above code. Note that you do not receive an error message.</li></ol>

Keywords: kbprb kbprod2web KB306134

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.