Problems using an XML Web service that returns a DataTable (306134)



The information in this article applies to:

  • Microsoft Web Services (included with the .NET Framework) 1.0
  • Microsoft Web Services (included with the .NET Framework 1.1)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2002)
  • Microsoft ASP.NET (included with the .NET Framework) 1.0
  • Microsoft Visual J# .NET (2002)
  • Microsoft Visual J# .NET (2003)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft ASP.NET (included with the .NET Framework 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 "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.

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.

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.

Modification Type:MajorLast Reviewed:10/25/2004
Keywords:kbprb kbProd2Web KB306134 kbAudDeveloper