There is a problem when using the XML web service that returns a DataTable

zhaozj2021-02-16  158

There is a problem when using the XML Web service that returns a DataTable.

The release number of this article has been CHS306134

symptom

In the case where the XML Web service returns a DataTable from its web method, if you browse to the URL of the XML web service, the following error message may appear:

System.Data.DataLation cannot serialize because it does not have a default public constructor.

When you try to set a web reference to the XML web service in the Visual Studio .NET Integration Development Environment (IDE), you may see an error message similar to the following:

internal server error. Unable to request "http://localhost/webservice1/service1.asmx? WSDL". The server responded to the error code "protocolerror".

the reason

DataTable, DataRow, DataView and DataViewManager objects cannot be serialized and cannot be returned from the XML Web service. To make the return result than the complete DataSet, you must copy the data to be returned to a new DataSet.

solution

To resolve this issue, you should return DataSet without returning DataTable. The DataSet object can contain one or more DataTable objects.

status

This phenomenon is designed to make.

More information

Steps to reproduce problems

Note: We provide code examples for Visual Basic .NET and Visual C # .NET.

Add a Web method that returns a DataTable to an existing XML web service. The following code creates a connection to the Microsoft SQL Server database and retrieves the Authors table. If you want to use this code, modify it to connect to your SQL Server computer. 'Visual Basic

Function givemeadataable () as system.data.dataable

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 givemeadataable ()

{

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"];

}

The following code creates a connection to the Microsoft SQL Server database and retrieves the Authors table. If you want to use this code, modify it to connect to your SQL Server computer. Compile XML web service. The following code creates a connection to the Microsoft SQL Server database and retrieves the Authors table. If you want to use this code, modify it to connect to your SQL Server computer. Browse to the URL of the XML web service that has just added code. Note that an error message will occur. The following code creates a connection to the Microsoft SQL Server database and retrieves the Authors table. If you want to use this code, modify it to connect to your SQL Server computer. Modify the code as shown below to return DataSet without returning DataTable: 'Visual Basic

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;

}

转载请注明原文地址:https://www.9cbs.com/read-9154.html

New Post(0)