我正在使用ChartJS上的线路图工作,并且我从WebService中遇到了一个错误。这是服务的代码:
[WebMethod]
public List<object> getProgram12Months(string usersessionid)
{
List<object> iData = new List<object>();
List<string> labels = new List<string>();
//First get distinct Month Name for select Year.
string query1 = "SELECT DISTINCT TOP (100) PERCENT TimeFrame FROM dbo.CSQ_ProgramCount12Months ORDER BY TimeFrame ";
string conn = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
SqlDataAdapter dap = new SqlDataAdapter(query1, conn);
DataSet ds = new DataSet();
dap.Fill(ds);
DataTable dtLabels = ds.Tables[0];
foreach (DataRow drow in dtLabels.Rows)
{
labels.Add(drow["TimeFrame"].ToString());
}
iData.Add(labels);
return iData;
}
当我从浏览器调用该方法时,我会收到以下错误:
system.invalidoperationException:生成错误 XML文档。---> system.invalidoperationException:类型 system.collections.generic.list'1 [[System.String,Mscorlib, 版本= 2.0.0.0,文化=中性,publicKeytoken = b77a5c561934e089]]]] 可能在这种情况下不使用。
我是基于此。
列表与Web方法无法序列化,而不是您可以返回对象[]。
[WebMethod]
public object[] getProgram12Months(string usersessionid)
{
List<object> iData = new List<object>();
List<string> labels = new List<string>();
//First get distinct Month Name for select Year.
string query1 = "SELECT DISTINCT TOP (100) PERCENT TimeFrame FROM dbo.CSQ_ProgramCount12Months ORDER BY TimeFrame ";
string conn = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
SqlDataAdapter dap = new SqlDataAdapter(query1, conn);
DataSet ds = new DataSet();
dap.Fill(ds);
DataTable dtLabels = ds.Tables[0];
foreach (DataRow drow in dtLabels.Rows)
{
labels.Add(drow["TimeFrame"].ToString());
}
iData.Add(labels.ToArray());
return iData.ToArray();
}
我通过创建两个类填充它们来解决此问题。谢谢您,您的帖子帮助我朝着正确的方向前进。
public class ChartData2
{
public List<string> Legends;
public List<int> AD;
}
public class Legend
{
public List<string> Months;
}