Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

Problems using an XML Web service that returns a DataTable


View products that this article applies to.

This article was previously published under Q306134

↑ Back to the top


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 "http://localhost/Webservice1/Service1.asmx?WSDL". The server responded with error code "ProtocolError".
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://<server>/vdir/service1.asmx was not recognized as a known document type.

↑ Back to the top


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.

↑ Back to the top


Resolution

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

↑ Back to the top


Status

This behavior is by design.

↑ Back to the top


More information

Steps to Reproduce the Behavior

NOTE: Sample code is provided for Visual Basic .NET, Visual C# .NET, and Visual J# .NET.
  1. 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
        <WebMethod()> Function GiveMeADataTable() As System.Data.DataTable
            Dim conn As New System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;")
            Dim ds As New System.Data.DataSet()
            Dim adapter As New System.Data.SqlClient.SqlDataAdapter()
            adapter.SelectCommand = New System.Data.SqlClient.SqlCommand("Select * From Authors", conn)
            adapter.Fill(ds, "Authors")
            Return ds.Tables("Authors")
        End Function
    					
    //Visual C#
       [WebMethod]
       public System.Data.DataTable GiveMeADataTable()
       {
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;");
          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("Select * From Authors", conn);
            adapter.Fill(ds, "Authors");
          return ds.Tables["Authors"];
       }    
    
    					
    /**Visual J# */ 
    	/** @attribute WebMethod() */ 
    	public System.Data.DataTable GiveMeADataTable()
    	{
    		System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;");
    		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("Select * From Authors", conn));
    		adapter.Fill(ds, "Authors");
    		System.Data.DataTableCollection dtc = ds.get_Tables();
    		return dtc.get_Item("Authors");
    	} 
    					
  2. Compile the XML Web service.
  3. Browse to the URL of the XML Web service in which you just added the code. Note that you receive an error message.
  4. Modify the code as follows to return a DataSet instead of a DataTable:
    'Visual Basic
        <WebMethod()> Function GiveMeADataSet() As System.Data.DataSet
            Dim conn As New System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;")
            Dim ds As New System.Data.DataSet()
            Dim adapter As New System.Data.SqlClient.SqlDataAdapter()
            adapter.SelectCommand = New System.Data.SqlClient.SqlCommand("Select * From Authors", conn)
            adapter.Fill(ds, "Authors")
            Return ds
        End Function
    					
    //Visual C#
       [WebMethod]
       public System.Data.DataSet GiveMeADataSet()
       {
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;");
          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("Select * From Authors", conn);
            adapter.Fill(ds, "Authors");
          return ds;
       }
    
    					
    /**Visual J# */ 
    /** @attribute WebMethod() */ 
    	public System.Data.DataSet GiveMeADataSet()
    	{
    		System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;");
    		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("Select * From Authors", conn));
    		adapter.Fill(ds, "Authors");
    		return ds;
    	}
    					
  5. Compile the XML Web service.
  6. 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.

↑ Back to the top


Keywords: kbprb, kbprod2web, KB306134

↑ Back to the top

Article Info
Article ID : 306134
Revision : 11
Created on : 10/25/2004
Published on : 10/25/2004
Exists online : False
Views : 411