Cannot Serialize The Data Table - Web Service

System.InvalidOperationException: There was an error generating the XML document. ---> 
System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set.

The error will occur when you create a method in web service (.asmx file) with return type "DataTable". For Example like below:

    [WebMethod]
    public DataTable test1()
    {
        DataTable testTable = new DataTable();
        return testTable;
    }


The Example above will lead to the Error "Cannot Serialize The Data Table"

This error because the method in WebService tries to return the result of DataTable to the client, but the DataTable does not have their own name. So the quick solution for this error has put the name for DataTable like example :

The Solutions

     [WebMethod]
     public DataTable test1()
    {
          DataTable testTable = new DataTable("Table1");
          return testTable;
     }

With this simple solution will solved the issue mentioned above.

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example