Link to home
Start Free TrialLog in
Avatar of volking
volking

asked on

Copy DataRows between in-memory Datasets

HOW TO - Copy DataRows between in-memory Datasets

        Dim dsIN As New Data.DataSet
... more code ...
        objDataAdapter.Fill(dsIN)

ok ... here I have a Dataset with ONE table
Now I want to DUPLICATE THE TABLE, ITS SCHEMA AND CONTENTS to a NEW dataset
I create the new dataset on-the-fly in-memory


        Dim dsCOPY As DataSet = New DataSet()
        dsCopy.Tables.Add(New DataTable("MyCopy"))
        For Each dc As DataColumn In dsIN.Tables(0).Columns
            dsCopy.Tables("MyCopy").Columns.Add(New DataColumn(dc.ColumnName, dc.DataType))
        Next

OK dsCOPY now duplicates the structure of dsIN
How do I copy all the records from dsIN into dsCopy
I thought this would work ... IT DOES NOT


        For Each drIN As DataRow In dsIN.Tables(0).Rows
            dsCopy.Tables(0).Rows.Add(drIN)
        Next

I get an error message on the dsCopy line ... "This row already belongs to another table."

Fred

ASKER CERTIFIED SOLUTION
Avatar of volking
volking

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of volking
volking

ASKER

Found the solution myself