I have a webservice that should return the top 5 emails in my inbox and display them in a data grid. I put my data into a DataTable first. But keep getting errors
Here is my code, Am I missing anything or declaring something wrong?
[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;
    }
Think the problem is public DataTable I had it declared as an object but that did not work ether .... sigh, what should I declare it as?
this is the error.... 
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set.
Assigning a value to dtMessages.DataTable name will stop the serialization error, as the error message suggests.
    [WebMethod]
    public DataTable GetDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Col1", typeof(string));
        dt.Rows.Add("testing");
        dt.TableName = "Blah";  // <---
        return dt;
    }
But I agree with Bob Horn that you're better off defining a class for your return value than using a DataTable.
Just give a table name when create a datatable object
DataTable dt = new DataTable("tablename");
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With