Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
4.00/5 (1 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:
C#
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
Posted
Updated 25-Mar-11 3:33am
v3
Comments
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...

Looks like this isn't possible:
http://support.microsoft.com/kb/306134[^]

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


Good luck!
 
Share this answer
 
Comments
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.
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.
 
Share this answer
 
Comments
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:

http://www.google.com/search?q=return+strongly+typed+dataset+from+webservice+&rls=com.microsoft:en-us&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1
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!..

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