locked
How to return Typed DataTable from a WebService? RRS feed

  • Question

  •  

       Hi all,
    I have a WinForm application and I also use a web service in this project. The problem is that;
    There is 3 project in the solution. I>Data layer, II>Service Layer, III>Presentation Layer. There is some of typed DataSet in Data Layer and I can fill and return this dataset to the presentation layer without any problem. On the other hand, when I try to return a datatable in this dataset, the datatable returns empty. I checked and obtain that; the typed datatable is filled in Data Layer and returns to Service layer. The datatable is still fill on the service layer. But, when return it to the application layer, it is getting empty. Here is the sample code I used. I think DataTables can be returned on webservices as DataSets. I use Typed Datatable but it should be returned as well. Am I wrong?

      On the Data Layer:  

      public StudentDS.EXAMSDataTable ExamTable()
      {
       SelectAll(dataSet.EXAMS); //This method fills the EXAMS table in dataset.
       return dataSet.EXAMS;
      }

       On the Service Layer:

      [WebMethod(Description = "Returns EXAMS datatable")]
      public StudentDS.EXAMSDataTable WsExamTable()
      {
       StudentDS.EXAMSDataTable dt = myDataAccess.ExamTable(); //Returns filled Exam table from Data Access Layer
       return dt; //NOTE : The table is filled at this stage
      }


       On the Presentation Layer:

      private void btFill_Click(object sender, EventArgs e)
      {
        dataGridView1.DataSource = myService.WsExamTable(); //NOTE : The table comes EMPTY from Service Layer at this stage
      }

     

    So, is there a mistake or how can I return filled datatable from a web service?

    BR
    Friday, March 25, 2011 1:50 PM

Answers

  • Hi Neddy,

    do you mean Service.asmx or Web.config? Could you clarify a little bit more please?


    Yes, the *.asmx which you want to connect. You should verify the *.asmx returns an datatable.

    I've look through your code above. It seems that you use a customer datatabel(StudentDS.EXAMSDataTable ), we don't know how does it be implemented. But as MSDN Library says:

    The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:
    ·The IList interface, including one-dimensional arrays.
    ·The IListSource interface, such as the DataTable and DataSet classes.
    ·The IBindingList interface, such as the BindingList<T> class.
    ·The IBindingListView interface, such as the BindingSource class.

    So, you must make sure that your customer dataTable has implemented one of the interfaces if you want to use a customer datatable.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Neddy Ren Wednesday, March 30, 2011 2:18 AM
    • Marked as answer by Neddy Ren Tuesday, April 5, 2011 1:56 AM
    Tuesday, March 29, 2011 1:49 AM
  • you can return serialized array. and try whatever Neddy Ren says

     

    Good luck ,, if you get the answer , just let us know

     

    • Proposed as answer by Neddy Ren Wednesday, March 30, 2011 2:18 AM
    • Marked as answer by Neddy Ren Tuesday, April 5, 2011 1:56 AM
    Tuesday, March 29, 2011 5:29 AM
  • Are you sure on Web Service it is not possible to return TYPEd Datatable? I can return Typed Dataset and I know I cannot return Datarow. But, I am not sure and no one is sure on this issue. So, if it is possible please show a simple method. If not, what is your suggestion? I think, if datatatable return is not possible I should add this TYPED Datatable to my TYPED Dataset and return this dataset. But when adding the TYPED Datatable to the TYPEd Dataset, there is an error like "the dataset already contains this datatatble". Therefore, if that is the only method to return dataset, could you inform me please how should I add the TYPEd Datatable that I want to return to the TYPED Dataset that I return from WebService?

    BR


    Please see the knowledge article:

    Problems using an XML Web service that returns a DataTable:
    http://support.microsoft.com/kb/306134

    The DataTable, DataRow, DataView, and DataViewManager objects cannot be serialized and cannot be returned from an XML Web service. To return less than a complete DataSet, you must copy the data that you want to return to a new DataSet.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 30, 2011 6:55 AM
  • Hi guys,

    Why is everybody helping with a question which has nothing to do with windows forms than that the result should be showed in a datagridview.

    This is either a problem in the webservice or in the data retrieving.

    But for sure not in this part which is windows forms.

     dataGridView1.DataSource = myService.WsExamTable(); //NOTE : The table comes EMPTY from Service Layer at this stage

    Because the table comes empty from the service so a question for 
    http://social.msdn.microsoft.com/Forums/en/wcf/threads

    Success
    Cor
    Wednesday, March 30, 2011 7:25 AM
  • myTypedDataSet.SchemaSerializationMode = SchemaSerializationMode.ExcludeSchema; 
    myTypedDataSet.RemotingFormat = SerializationFormat.Binary; 

    Soap is not the most efficient way of getting data across, but it has its merits. If you have huge datasets filled with tons of data, then you'll notice a substantial delay, but then that would be very exceptional in a normal client application. The fact that your dataset is typed doesn't make any difference in the serialized data that is sent, it's the class definition that is extended, not the data. For data that doesn't need to be updated and posted back to the server, you can send it as an array of some sort. Sending datasets over is already quite optimized, I wouldn't try and tweak the dataset; you only risk messing things up. Soap works with xml serilization, so Binary won't work. If you really want flexibility, you should use WCF, but that is less straightforward to set up.
    • Proposed as answer by Neddy Ren Friday, April 1, 2011 1:57 AM
    • Marked as answer by Neddy Ren Tuesday, April 5, 2011 1:58 AM
    Thursday, March 31, 2011 11:48 AM

All replies

  • Is that full on the service side? Maybe it's just not populated or DB does not contain any data?
    Don't forget to mark the correct answer Blog
    Friday, March 25, 2011 2:49 PM

  • Is that full on the service side? Maybe it's just not populated or DB does not contain any data?
    Don't forget to mark the correct answer Blog
    Please look at the line On the Service Layer:
    return dt; //NOTE : The table is filled at this stage
    Friday, March 25, 2011 2:57 PM
  • And the service is WCF Data Service, correct?
    Don't forget to mark the correct answer Blog
    Friday, March 25, 2011 4:38 PM
  • And the service is WCF Data Service, correct?
    Don't forget to mark the correct answer Blog
    No, I use web service for my click once application...
    Friday, March 25, 2011 9:57 PM
  • That could be the reason, so the service layer does not know how to serialize data objects.

    Refer to odata and WCF data services


    Don't forget to mark the correct answer Blog
    Saturday, March 26, 2011 2:50 AM
  • That could be the reason, so the service layer does not know how to serialize data objects.

    Refer to odata and WCF data services


    Don't forget to mark the correct answer Blog
    But as I said before, I do not use WCF...
    Sunday, March 27, 2011 4:54 PM
  • Hi H.Johnson,

    Have you opened the link of the webServices? In the page, you can find and exact that what the web service return? A dataTable type or a DataSet type? You should make sure you have got the correct return type. Please check it first! 

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 28, 2011 6:03 AM
  • Hi H.Johnson,

    Have you opened the link of the webServices? In the page, you can find and exact that what the web service return? A dataTable type or a DataSet type? You should make sure you have got the correct return type. Please check it first! 

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Hi Neddy,

    do you mean Service.asmx or Web.config? Could you clarify a little bit more please?

     

    Monday, March 28, 2011 3:15 PM
  • Hi Neddy,

    do you mean Service.asmx or Web.config? Could you clarify a little bit more please?


    Yes, the *.asmx which you want to connect. You should verify the *.asmx returns an datatable.

    I've look through your code above. It seems that you use a customer datatabel(StudentDS.EXAMSDataTable ), we don't know how does it be implemented. But as MSDN Library says:

    The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:
    ·The IList interface, including one-dimensional arrays.
    ·The IListSource interface, such as the DataTable and DataSet classes.
    ·The IBindingList interface, such as the BindingList<T> class.
    ·The IBindingListView interface, such as the BindingSource class.

    So, you must make sure that your customer dataTable has implemented one of the interfaces if you want to use a customer datatable.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Neddy Ren Wednesday, March 30, 2011 2:18 AM
    • Marked as answer by Neddy Ren Tuesday, April 5, 2011 1:56 AM
    Tuesday, March 29, 2011 1:49 AM
  • you can return serialized array. and try whatever Neddy Ren says

     

    Good luck ,, if you get the answer , just let us know

     

    • Proposed as answer by Neddy Ren Wednesday, March 30, 2011 2:18 AM
    • Marked as answer by Neddy Ren Tuesday, April 5, 2011 1:56 AM
    Tuesday, March 29, 2011 5:29 AM
  • Hi again,

    actually I want to return TYPED Datatable rather than Datatble from web serice. on the other hand, I have tried some combination to return both of them and I have tried Boxing when I return Datatable like (MYTYPEDDATATABLE)service.GetDataTable();

    But, on all cases, the datatable or TYPED DATATABLE is full on service layer and when just after return it to the presentation layer, the table gets empty. So, there is very confusing on the web forms. Some said "yes, the Datatable (or TYPED Datatable) an be returned from web services, and some said "no, it cannot be returned!..". So, which one is true? Is it  too hard questions to get an answer fo weeks? I am really want to know if it is possible, I will apply this method. If not, I will continues to return TYPED DATASET instead of TYPED Datatable. But in this case, there are some unnecessary components on Dataset and I want to return just a single datatable instead of dataset.

    BR

    Tuesday, March 29, 2011 11:32 AM
  • if you want to return CLR type , you should use WCF , so for your situation you need to use data array , because Webservice is not accepting the CLR type. so you need to change it to lower type that it can accept.
    • Proposed as answer by Neddy Ren Wednesday, March 30, 2011 2:18 AM
    • Unproposed as answer by H.Johnson Wednesday, March 30, 2011 6:35 AM
    Tuesday, March 29, 2011 2:58 PM
  • Are you sure on Web Service it is not possible to return TYPEd Datatable? I can return Typed Dataset and I know I cannot return Datarow. But, I am not sure and no one is sure on this issue. So, if it is possible please show a simple method. If not, what is your suggestion? I think, if datatatable return is not possible I should add this TYPED Datatable to my TYPED Dataset and return this dataset. But when adding the TYPED Datatable to the TYPEd Dataset, there is an error like "the dataset already contains this datatatble". Therefore, if that is the only method to return dataset, could you inform me please how should I add the TYPEd Datatable that I want to return to the TYPED Dataset that I return from WebService?

    BR

    Wednesday, March 30, 2011 6:49 AM
  • Are you sure on Web Service it is not possible to return TYPEd Datatable? I can return Typed Dataset and I know I cannot return Datarow. But, I am not sure and no one is sure on this issue. So, if it is possible please show a simple method. If not, what is your suggestion? I think, if datatatable return is not possible I should add this TYPED Datatable to my TYPED Dataset and return this dataset. But when adding the TYPED Datatable to the TYPEd Dataset, there is an error like "the dataset already contains this datatatble". Therefore, if that is the only method to return dataset, could you inform me please how should I add the TYPEd Datatable that I want to return to the TYPED Dataset that I return from WebService?

    BR


    Please see the knowledge article:

    Problems using an XML Web service that returns a DataTable:
    http://support.microsoft.com/kb/306134

    The DataTable, DataRow, DataView, and DataViewManager objects cannot be serialized and cannot be returned from an XML Web service. To return less than a complete DataSet, you must copy the data that you want to return to a new DataSet.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 30, 2011 6:55 AM
  • Hi guys,

    Why is everybody helping with a question which has nothing to do with windows forms than that the result should be showed in a datagridview.

    This is either a problem in the webservice or in the data retrieving.

    But for sure not in this part which is windows forms.

     dataGridView1.DataSource = myService.WsExamTable(); //NOTE : The table comes EMPTY from Service Layer at this stage

    Because the table comes empty from the service so a question for 
    http://social.msdn.microsoft.com/Forums/en/wcf/threads

    Success
    Cor
    Wednesday, March 30, 2011 7:25 AM
  • @Neddy : Thanks for answer. Actually I had looked this article but because I use TYPEd Datatble, my si,tuation may be different from the situation on it. Because when I try to return Datatable, I get an error like "there is an error on xml document (...,...)". But when I try to return TYPED Datatable, there is no error and table returns. But unfortunately EMPTY. I debug step by step and I am sure that; the tables is full on SERVICE LAYER. After returning to APPLICATION LAYER, the table (TYPED) gets EMPTY. On the other hand, there are a lof of approach on the web I have searched for tens of pages. There are not an clear solution or methods to solve problem. If I were sure that it is not possible to return TYPED Datatble from Web Service, I would started to return TYPED DATASEt instead of Datatble. But, from all the forums and articles it is not clear and I want to be clarified if it is possible or not.

     

    @Cor : Thanks for reply and I agrre with you :) But, I do not use WCF. So, I need a solution for web service on .NET 3.5. ü

    Thanks in advance...

    Wednesday, March 30, 2011 8:54 AM
  • The WCF forum is for webservices

    Windows Communication Foundation is a set of .NET technologies for building and running connected systems. It is a new breed of communications infrastructure built around the Web services architecture.


    Success
    Cor
    Wednesday, March 30, 2011 9:00 AM
  • The WCF forum is for webservices

     

    Windows Communication Foundation is a set of .NET technologies for building and running connected systems. It is a new breed of communications infrastructure built around the Web services architecture.

     


    Success
    Cor

    What about using WCF instead of Web Service (*.asmx)? I built my methods using a Web Service and if you suggest WCF rather than WebService should I change to WCF from Web Service? Is it difficult to switch or what should I take into account?

    BR

    Wednesday, March 30, 2011 12:36 PM
  • H Johnson,

    I've copied the text from MSDN.

    Follow that link I gave you

    (new name) WCF = (for webservice)(old name) Webservice

    This forum is for the System Windows Forms Namespace 

     


    Success
    Cor
    Wednesday, March 30, 2011 5:18 PM
  • How to return Typed DataTable from a WebService? return the dataset instead of only the table.
    • Proposed as answer by Neddy Ren Thursday, March 31, 2011 4:41 AM
    • Unproposed as answer by H.Johnson Thursday, March 31, 2011 1:09 PM
    Wednesday, March 30, 2011 10:57 PM
  • How to return Typed DataTable from a WebService? return the dataset instead of only the table.

    Well,

    I can return TYPEd Dataset but as I said before there is a lot of unnecessary objects in it. So, how can I return TYPED Dataset most effectively (containing only the tables I need, not any unnecessary xml schema, relation, etc.)? There is some kind of lines below and if there is some precautions to reduce the returned TYPED Dataset size before returning Dataset from Service Layer. Couldyou inform me please?

          myTypedDataSet.SchemaSerializationMode = SchemaSerializationMode.ExcludeSchema;
          myTypedDataSet.RemotingFormat = SerializationFormat.Binary;
    
          return myTypedDataSet;

    Thursday, March 31, 2011 10:52 AM
  • myTypedDataSet.SchemaSerializationMode = SchemaSerializationMode.ExcludeSchema; 
    myTypedDataSet.RemotingFormat = SerializationFormat.Binary; 

    Soap is not the most efficient way of getting data across, but it has its merits. If you have huge datasets filled with tons of data, then you'll notice a substantial delay, but then that would be very exceptional in a normal client application. The fact that your dataset is typed doesn't make any difference in the serialized data that is sent, it's the class definition that is extended, not the data. For data that doesn't need to be updated and posted back to the server, you can send it as an array of some sort. Sending datasets over is already quite optimized, I wouldn't try and tweak the dataset; you only risk messing things up. Soap works with xml serilization, so Binary won't work. If you really want flexibility, you should use WCF, but that is less straightforward to set up.
    • Proposed as answer by Neddy Ren Friday, April 1, 2011 1:57 AM
    • Marked as answer by Neddy Ren Tuesday, April 5, 2011 1:58 AM
    Thursday, March 31, 2011 11:48 AM