简体   繁体   English

C#-WebService Soap异常

[英]C#-WebService Soap Exception

I am trying to retrieve a list of dropdown items from the database. 我正在尝试从数据库中检索下拉列表。 The code works good if I am not using a Web Service to access the database, but when I used webservice to access the database, it gives me a Soap Exception. 如果我不使用Web服务访问数据库,则代码运行良好,但是当我使用Web服务访问数据库时,则出现Soap Exception。 Here is the code, please help. 这是代码,请帮助。

The Exception is: 例外是:

System.Web.Services.Protocols.SoapException: Server was unable to process request. --->
System.InvalidOperationException: There was an error generating the XML document. --->
System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set.
    at System.Data.DataTable.WriteXmlSchema(XmlWriter writer, Boolean writeHierarchy)

Here is the Method: 这是方法:

private void retrieveStates()
        {
            dataService = new PCDDA.Service();
            DataTable dt = new PCDDA.Service().GetStates();
            DDLSelectState.DataSource = dt;
            DDLSelectState.DataTextField = "RegionName";
            DDLSelectState.DataValueField = "RegionID";
            DDLSelectState.DataBind();
            DDLSelectState.Items.Insert(0, new ListItem("<Select State>", "0"));
        }

Here is the GetStates() Web Method which is in the Service.cs class of the Web Service: 这是Web Service的Service.cs类中的GetStates()Web方法:

[WebMethod]
    public DataTable GetStates()
    {
        DbPostcardOTR db = new DbPostcardOTR();
        try
        {
            DataTable loadstates = db.LoadStates();
            return loadstates;

        }
        catch (Exception ex)
        {
            throw new Exception("Unable to retrieve or load States from the database. The Exception is :" + ex.Message);
        }

    }

This is the LoadStates() Method in the DbPostcardOTR.cs: 这是DbPostcardOTR.cs中的LoadStates()方法:

 public DataTable LoadStates()
    {
        DataTable States = new DataTable();
        SqlConnection con = OpenConnection();
        try
        {
            string selectSQL = "Select RegionID, RegionName from PCDDev.dbo.tblDistributionArea";
            SqlCommand cmd = new SqlCommand(selectSQL, con);
            SqlDataReader dr = cmd.ExecuteReader();
            States.Load(dr);
            return States;
        }
        finally
        {
            if (con != null)
                con.Close();
        }
    }

From Microsoft: 从Microsoft:

The DataTable, DataRow, DataView, and DataViewManager objects cannot be serialized and cannot be returned from an XML Web service. DataTable,DataRow,DataView和DataViewManager对象无法序列化,也不能从XML Web服务返回。 To return less than a complete DataSet, you must copy the data that you want to return to a new DataSet. 要返回的数据少于完整的DataSet,必须将要返回的数据复制到新的DataSet。

Source: Problems using an XML Web service that returns a DataTable 来源: 使用返回数据表的XML Web服务时遇到的问题

Note: The above source contains an alternative which may work in your situation. 注意:以上来源包含一种可能适合您的情况的替代方法。 In a nutshell, return a DataSet. 简而言之,返回一个DataSet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM