我正在尝试从数据库中检索下拉项目列表。如果我不使用 Web 服务访问数据库,该代码运行良好,但是当我使用 Web 服务访问数据库时,它给了我一个 Soap 异常。这是代码,请帮助。
例外是:
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();
}
}