title: How to return a Datatable by a [WebMethod]
body: <p>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 <code>DataTable</code> first. But keep getting errors</p>
  
  <p>Here is my code, Am I missing anything or declaring something wrong?</p>
  
  <pre><code>[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 &gt;= 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 &gt; 5)
              {
                  break;
              }
          }
          return dtMessages;
      }
  </code></pre>
  
  <p>Think the problem is public <strong>DataTable</strong> I had it declared as an object but that did not work ether .... sigh, what should I declare it as?<br>
  this is the error.... </p>
  
  <blockquote>
    <p>System.InvalidOperationException: There was an error generating the
    XML document. ---> System.InvalidOperationException: Cannot serialize
    the DataTable. DataTable name is not set.</p>
  </blockquote>