table into another dataset

  • Thread starter Thread starter Marty McDonald
  • Start date Start date
M

Marty McDonald

I execute a method that returns a dataset, but I want the table in another
dataset. Clone won't work because it does not include data. Can I somehow
use the existing table in another dataset without getting the "table already
belongs to another dataset" error? Thanks!
--Marty
 
Hi Marty,

Are you able to use Copy? Maybe I've misunderstood your issue, but this
seems to work for me:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim ds As New DataSet
Dim ds2 As New DataSet
Dim dt As New DataTable
Dim dtsrc As New datasrc
dt = dtsrc.CreateDataSource()
ds.Tables.Add(dt)
ds2.Tables.Add(dt.Copy)
DataGrid1.DataSource = ds2
DataGrid1.DataBind()
End Sub
 
Hi Marty McDonald,

Thanks for using Microsoft NewsGroup Services. Based on your description,
you want to copy a DataTable in one DataSet into another DataSet( not only
the structure but also with the datas) so as to do some operations on the
new copied DataTable. Is my understanding of your problem correct?

If so, I think Ken Cox's suggestion is good, you may try the DataTable's
copy method which will generate a same DataTAble as the origianl one with
not only structure but datas as well. Such as :

[Visual Basic]
Private Sub CopyDataTable(ByVal myDataTable As DataTable )
' Create an object variable for the copy.
Dim copyDataTable As DataTable
copyDataTable = myDataTable.Copy()
' Insert code to work with the copy.
End Sub
[C#]
private void CopyDataTable(DataTable myDataTable){
// Create an object variable for the copy.
DataTable copyDataTable;
copyDataTable = myDataTable.Copy();
// Insert code to work with the copy.
}

Please try out the suggestion. If you have any question on it ,please feel
free to let me know.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Yes Steven, you are correct, I want the table (structure & data) from one
dataset copied into another dataset. You and Ken are correct, the Copy
method solves the problem. I just did not look closely enough at the
available methods, thank you for the information! --Marty
 
That causes the error about the table already belonging to another dataset.
We did find the correct answer though, it is posted in this thread. Thanks
for your help!
 
Back
Top