Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
I just want to know if it is possible to use DataTable in Web Service as na output. how can I do this?
Posted

try this

return a dataset instead. you need proper XML

Thanks
hemant
 
Share this answer
 
DataTables are XML serializable, and so you can use them as both input and output from an XML Web Service. Here is an article from MSDN Consuming a DataSet from an XML Web Service[^]

Hope this helps
 
Share this answer
 
You can return DataTable in WebService.
You just need to give full DataTable name.

e.g
DataTable dt = new DataTable("YourTableName");

That's all.

To return object in web service, Microsoft need to serialize the object.
At that time your data table object must has name. So, Microsoft will convert to XML like
<yourtablename>
<------
----->
<yourtablename>
If your table has no name, Microsoft cannot serialize as XML file. So, will be throw error.

Thanks
 
Share this answer
 
Comments
ALA'A 7-Nov-12 4:17am    
thank you very much itmgmg844
No, You can't return a DataTable object from a webservice method.

If you try, ends up with the following message

"System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set. at System.Data.DataTable.WriteXmlSchema(XmlWriter writer, Boolean writeHierarchy)

at System.Data.DataTable.System.Xml.Serialization.IXmlSerializable.WriteXml(XmlWriter writer)
at System.Xml.Serialization.XmlSerializationWriter.WriteSerializable(IXmlSerializable serializable, String name, String ns, Boolean isNullable, Boolean wrapped)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_DataTable(Object o)
at Microsoft.Xml.Serialization.GeneratedAssembly.DataTableSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
at System.Web.Services.Protocols.WebServiceHandler.Invoke()"

If you look into the message, we can find that DataTable is not serializable. Or in other words, since DataTable doesn't implement the ISerializable interface.
It can't be used as a return type for a webserivice method.

Alternatively, create you own class which inherits ISerializable interface. And pass the data using that newly created object. Or create the data in XML format
and pass it as a string.


Try out below code ,

Web service:

        [WebMethod]
        public DataTable HelloWorldDataSet()
        {
            DataTable dt = new DataTable("MyDataTable");
            dt.Columns.Add("column1",typeof(System.String));
            dt.Columns.Add("column2", typeof(System.String));

            DataRow dr = dt.NewRow();
            dr["column1"] = "Your Data";
            dr["column2"] = "Your Data";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["column1"] = "Your Data";
            dr["column2"] = "Your Data";
            dt.Rows.Add(dr);

            return dt;
        }

Client code:

        private void button1_Click(object sender, EventArgs e)
        {
            helloservice.Service1 service = new WindowsFormsApplication1.helloservice.Service1();
            DataTable dt = service.HelloWorldDataSet();
        }


Or you can return Dataset instead of dataTable refer this link
[^]

I Hope it will help you,Enjoy....
 
Share this answer
 
v2
Comments
cuteband 10-May-11 4:42am    
As per Wayne we can use Dataset Instead so this will help for me..
thams 10-May-12 8:01am    
Hi,
It s simple.
If you need to serialize a data table then you have to give a name for that.
In your code just assign a name to the table.Then it'll work like a charm.
dt.name = "summa";
i have done this today, so here a complete code share as per your problem.

C#
[WebMethod]
        public DataTable _dataConnection()
        {
            DataTable dt = new DataTable("mytable");
            try
            {
                com = new SqlCommand("select * from Dep", con);
                con.Open();
                SqlDataReader rd = com.ExecuteReader();
                dt.Load(rd);
                con.Close();

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
                {
                    con.Close();
                }
        }


hit for more confusion reply button.
 
Share this answer
 
Comments
Member 11470535 4-Mar-15 2:26am    
i can not use DataTable syntax!!!!!!!!
datatable dt;
//---------
//-----------
dataset ds= new dataset();
ds=dt.tables[0];
return dt;
 
Share this answer
 
Comments
CHill60 13-Mar-15 9:58am    
The question is over 3 years old and already answered - to a far better level than this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900