Click here to Skip to main content
13,089,943 members (42,667 online)
Rate this:
Please Sign up or sign in to vote.
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:
<br />
<pre lang="c#">public StudentDS.EXAMSDataTable ExamTable()<br />
        {<br />
            SelectAll(dataSet.EXAMS); //This method fills the EXAMS table in dataset.<br />
            return dataSet.EXAMS;<br />
        }</pre><br />

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?
Posted 24-Mar-11 23:04pm
Updated 25-Mar-11 3:33am
willempipi 25-Mar-11 10:01am
[WebMethod(Description = "Returns EXAMS datatable")]
public StudentDS.EXAMSDataTable WsExamTable()
StudentDS.EXAMSDataTable dt = myDataAccess.ExamTable(); //Returns filled Exam table from Data Access Layer << DID YOU SEE THAT IN DEBUG ???!!
return dt; //NOTE : The table is filled at this stage

If not; Use Nijboer's solution and serialize the datatable yourself, Webservices are only capable of transfering simple datatypes.
H.Johnson 25-Mar-11 10:56am
I have known, but I am wondering wherher the webservices can return DataTable? If yes,
how can I do this? I tried Nijboer's method but it does not make any sense. So, the returned datatable is empty...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Looks like this isn't possible:[^]

You could convert it to xml something like this:
DataTable dt = myDataAccess.ExamTable();              
XmlDocument XMLDoc = new XmlDocument();

Good luck!
H.Johnson 25-Mar-11 10:02am
It does not work unfortunately...
willempipi 25-Mar-11 10:58am
I expected that, if this code would have worked you own code would have worked too.

Serialize and Deserialize it yourself, by hand, or try to rewrite your derived class so it can serialize.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can pass a DataSet through the Web Service but not a DataTable. Just leave it in the DataSet. The DataSet is already serializeable, but a DataTable is not. Go figure.
H.Johnson 25-Mar-11 17:54pm
Well, actually I have managed to return Typed Dataset before without any problem. But, in this case there are a lot of unnecessary datarelations, etc. in returned dataset. So, how can I return a Typed Dataset which contains just a datatable without any unnecessary dataset elements? Could you give most suitable way to achieve this please?
On the other hand, I use .Net3.5 and if it is possible to return strongly typed dataset from webservice (I think there may be a way), give an example please?
Thanks in advance...
Kschuler 28-Mar-11 9:16am
If it were me, I'd probably just create a new clean DataSet, add the DataTable to it, and return that one. I've never really delved into trying to figure out how much overhead was in one because I've never run into complaints about it with my program before. If it's really a problem for you, I guess you'll have to use the suggesion in Solution 1. As for a way to return a strongly typed dataset from a webservice, google seems to have quite a few tips for you:
H.Johnson 29-Mar-11 7:49am
I have searched on Google so much before but most of them seem to give similar suggestions like you without any solution. Instead of this, I need a solution because there ara a lot of suggestions on Gooogle.
Kschuler 29-Mar-11 9:28am
Why don't you start working on it, and when you run into a problem post another question to help solve it. We can't help you debug an issue if we don't know what code you are using. And no one is going to just do your work for you. Start by changing your webmethod to return a DataSet and go from there.
H.Johnson 29-Mar-11 10:59am
I have worked for days on this issue and already managed to return Typed Dataset and there is no problem. If you do not know if it is possible or not to return a Typed Datatable from a web service, I do not request any answer from you. If know, you can show a little method or trick regarding to this issue. I do not need you help to complete my project but everyone may need helps on some points including you!..
Kschuler 29-Mar-11 11:02am
I thought you were asking for samples on how to return a DataSet. From my understanding it is not possible to return a DataTable, which is why I do not have any examples of that for you.
H.Johnson 30-Mar-11 2:36am
There are a lot of approach on the web and half of them said "yes, it is possible" and the rest said "no". Now, both of them may be true but I just want to be clarified if it is possible, how can it be done? If not, what should be done in order to return just a SINGLE Typed Datatable in a TYPED Dataset? My aim is that; I can return Typed Dataset but there is a lot of unecessary object in it (relations, etc.). So, I need to return this dataset with only my Typed Datatble that I need. Because, other case I return unnecessary object in returned Dataset that I should avoid. Is there any suggestion regarding to this issue? I tried to create a new TYPED Dataset in a Method on Service Layer and I add the Typed Datatable on the Dataset before returning to the Presentation Layer. But I am not sure if it is the logical way.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 25 Mar 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100