bryker
July 22nd, 2002, 03:59 PM
I have an XML schema which defines my DataSet. So, I deal with a MyDataSet type, which inherits from Data.DataSet, and a MyDataTable, which inherits from Data.DataTable. These classes were created using the .NET IDE's "Generate Dataset" function, to create all these strongly-typed classes that mirror the structure of my XSD.
I have a WebMethod in a Web Service which I desperately want to return one of these MyDataTable objects as a value. The MyDataTable object I want to return is part of a MyDataSet object--but I only want to return this single MyDataTable object. My WinClient.EXE needs to receive this MyDataTable object, one way or another. Here's what I've run up against so far:
[1] My first idea was to have the WebMethod (let's call it WebFoo()) return a type of MyDataTable. This seems the most obvious route. But when I try to update the Web Reference to WebFoo() from my WinClient, then it complains about "no default constructor for MyDataTable". I don't know how I would address this problem, so I move on.
[2] Then I thought, hey, these DataSet classes (both base and derived) have a handy .GetXml() method, which claims to return String data representing the DataSet. So I tried this in WebFoo():
<WebMethod(MessageName:="WebFoo")> _
Public Function WebFoo( _
ByVal sSomeKey As String _
) _
As String
Dim oNewDataSet As New MyDataSet()
'// First, we need to remove any (empty) table by the name of the one
'// that we're about to add. Otherwise, the Tables.Add() call below
'// will blow sky-high.
If (oNewDataSet.Tables.Contains(OldDataSet.MyDataTable.TableName)) Then
Call oNewDataSet.Tables.Remove(OldDataSet.MyDataTable.TableName)
End If
'// If I don't use the .Clone() method here, execution fails with a
'// message to the effect that, "MyDataTable is already a Table in
'// the OldDataSet DataSet."
'// On the other hand, this .Clone method doesn't even include
'// any rows--just the DataTable's structure. Aaaaaugggh!
Call oNewDataSet.Tables.Add(OldDataSet.MyDataTable.Clone)
Dim sReturnValue As String = oNewRMXDataSet.GetXml()
'// The only thing returned here is this:
'// <MyDataSet xmlns="http://tempuri.org/MyDataSet.xsd" />
Return sReturnValue
End Function
See my comments in the above code--I'm hamstrung by [a] the Tables.Add() method (which seems to want the result of the DataTable.Clone() method), [b] the DataTable.Clone() method, which seems to ignore row data, and only clones the structure, and [c] the DataSet.GetXml() method, which does not return the whole DataSet, but only the first line thereof.
[3] DarthPedro suggested I just return the MyDataSet object (since this would indeed have the default constructor necessary), but I still have to add the MyDataTable object to that return-value MyDataSet object before trying to return it, and I can't add MyDataTable to it because no rows get added. Yeeesh. I know I have to be making this harder than it really should be.
I guess I could loop through each row in the OldDataSet's MyDataTable, adding each one to the NewDataSet's MyDataTable, but at some point I have to think there's an easier (cleaner, more elegant) way of doing all this that I just don't know about. Anyone out there know of such a way?
Thanks a lot for any insight on this.
I have a WebMethod in a Web Service which I desperately want to return one of these MyDataTable objects as a value. The MyDataTable object I want to return is part of a MyDataSet object--but I only want to return this single MyDataTable object. My WinClient.EXE needs to receive this MyDataTable object, one way or another. Here's what I've run up against so far:
[1] My first idea was to have the WebMethod (let's call it WebFoo()) return a type of MyDataTable. This seems the most obvious route. But when I try to update the Web Reference to WebFoo() from my WinClient, then it complains about "no default constructor for MyDataTable". I don't know how I would address this problem, so I move on.
[2] Then I thought, hey, these DataSet classes (both base and derived) have a handy .GetXml() method, which claims to return String data representing the DataSet. So I tried this in WebFoo():
<WebMethod(MessageName:="WebFoo")> _
Public Function WebFoo( _
ByVal sSomeKey As String _
) _
As String
Dim oNewDataSet As New MyDataSet()
'// First, we need to remove any (empty) table by the name of the one
'// that we're about to add. Otherwise, the Tables.Add() call below
'// will blow sky-high.
If (oNewDataSet.Tables.Contains(OldDataSet.MyDataTable.TableName)) Then
Call oNewDataSet.Tables.Remove(OldDataSet.MyDataTable.TableName)
End If
'// If I don't use the .Clone() method here, execution fails with a
'// message to the effect that, "MyDataTable is already a Table in
'// the OldDataSet DataSet."
'// On the other hand, this .Clone method doesn't even include
'// any rows--just the DataTable's structure. Aaaaaugggh!
Call oNewDataSet.Tables.Add(OldDataSet.MyDataTable.Clone)
Dim sReturnValue As String = oNewRMXDataSet.GetXml()
'// The only thing returned here is this:
'// <MyDataSet xmlns="http://tempuri.org/MyDataSet.xsd" />
Return sReturnValue
End Function
See my comments in the above code--I'm hamstrung by [a] the Tables.Add() method (which seems to want the result of the DataTable.Clone() method), [b] the DataTable.Clone() method, which seems to ignore row data, and only clones the structure, and [c] the DataSet.GetXml() method, which does not return the whole DataSet, but only the first line thereof.
[3] DarthPedro suggested I just return the MyDataSet object (since this would indeed have the default constructor necessary), but I still have to add the MyDataTable object to that return-value MyDataSet object before trying to return it, and I can't add MyDataTable to it because no rows get added. Yeeesh. I know I have to be making this harder than it really should be.
I guess I could loop through each row in the OldDataSet's MyDataTable, adding each one to the NewDataSet's MyDataTable, but at some point I have to think there's an easier (cleaner, more elegant) way of doing all this that I just don't know about. Anyone out there know of such a way?
Thanks a lot for any insight on this.