我有一个网络服务,应该返回收件箱中的前5个电子邮件,并将它们显示在数据网格中.我首先将数据放入DataTable.但不断收到错误
这是我的代码,我错过了什么或者说错了吗?
[WebMethod] public DataTable DisplayMailList(String inMailServer,String inPort,bool inSSlCheck,String inUsername,String inPassword) { objClient.Connect(inMailServer,int.Parse(inPort),inSSlCheck); objClient.Authenticate(inUsername,inPassword); int count = objClient.GetMessageCount(); DataTable dtMessages = new DataTable(); // Creating datatable. dtMessages.Columns.Add("MessageNumber"); dtMessages.Columns.Add("From"); dtMessages.Columns.Add("Subject"); dtMessages.Columns.Add("DateSent"); dtMessages.TableName = "dtMessages"; int counter = 0; for (int i = count; i >= 1; i--) { OpenPop.Mime.Message msg = objClient.GetMessage(i); dtMessages.Rows.Add(); dtMessages.Rows[dtMessages.Rows.Count - 1]["MessageNumber"] = i; //Populateing Datatable dtMessages.Rows[dtMessages.Rows.Count - 1]["Subject"] = msg.Headers.Subject; dtMessages.Rows[dtMessages.Rows.Count - 1]["DateSent"] = msg.Headers.DateSent; counter++; if (counter > 5) { break; } } return dtMessages; }
认为问题是公共DataTable我把它声明为一个对象但是没有工作以太……叹了口气,我应该将它声明为什么?
这是错误….
System.InvalidOperationException: There was an error generating the
XML document. —> System.InvalidOperationException: Cannot serialize
the DataTable. DataTable name is not set.