我正在使用Chartjs处理折线图,并且从Web服务中收到错误消息。这是该服务的代码:
[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]] 在这种情况下可能无法使用。
我以off this为基础。
答案 0 :(得分:2)
列表无法序列化为Web方法,相反,您可以返回object []。
[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();
}
答案 1 :(得分:2)
我通过创建两个类并填充它们来解决此问题。谢谢您,您的帖子帮助我朝着正确的方向前进。
public class ChartData2
{
public List<string> Legends;
public List<int> AD;
}
public class Legend
{
public List<string> Months;
}