How to return a Datatable by a [WebMethod]

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");