我正在尝试从数据库中检索下拉列表项。如果我没有使用Web服务访问数据库,代码工作正常,但是当我使用webservice访问数据库时,它给了我一个Soap Exception。这是代码,请帮忙。
例外是:
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)
以下是方法:
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"));
}
以下是Web服务的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);
}
}
这是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();
}
}
答案 0 :(得分:1)
来自Microsoft:
DataTable,DataRow,DataView和DataViewManager对象不能 是序列化的,不能从XML Web服务返回。至 返回少于完整的DataSet,您必须复制您的数据 想要返回一个新的DataSet。
来源:Problems using an XML Web service that returns a DataTable
注意:上述来源包含可能适用于您的情况的替代方案。简而言之,返回一个DataSet。