Returning DataTable objects with WCF service

后端 未结 3 1592
感动是毒
感动是毒 2020-12-21 00:44

This is a strange problem that I am having with WCF trying to send DataTable in the response. I have the following service contract:

[ServiceContract]
public         


        
3条回答
  •  借酒劲吻你
    2020-12-21 01:01

    I know this is an old question, but maybe someone is still experiencing the same problem (like I did). I was faced with the same confusing error message and spent hours banging my head when it hit me....

        public DataTable GetDataTable()
        {
            DataTable dt = new DataTable(**"MY_NAME"**);
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Caption", typeof(string));
    
            dt.Rows.Add(new object[] { 1, "hooray!" });
            return dt;
        }
    

    you should give your DataTable a name, i used a overloaded constructor and no more errors!

    EDIT: Of course, you really shouldn't use DataTable or DataSet as a return type from a WCF service. For me it was just for testing purposes, because I thought it's the quickest way to get something out of a database and over the wire....boy was i wrong :)

提交回复
热议问题