Error duplicating records into datatable

japryor

New member
Joined
Apr 14, 2008
Messages
3
Programming Experience
5-10
I've created a DataTable and would like to load it with 30 copies of the same record. I'm trying the following code, but I keep getting an error "The record is already belongs to this table". I'm guessing the drow object is just a pointer and the pointer is getting copied, not the datarow. Any thoughts?

VB.NET:
        Dim drow As DataRow = LabelDS.Tables(0).Rows.Item(0)

        For i = 0 To 29
            LabelDS.Tables(0).NewRow()
            LabelDS.Tables(0).Rows.Add(drow)
        Next
-Jason
 
Solved...

Yeah, it must have been something to do with copying references instead of data. I had to create a blank row, then iteratively set each field in the new row equal to a field from the original row. Here's the code that works...

VB.NET:
        For i = 0 To 29
            Dim drow As DataRow = LabelDS.DataTable1.NewRow()
            For x = 0 To 8
                drow.Item(x) = LabelDS.DataTable1.Rows.Item(0).Item(x)
            Next
            LabelDS.DataTable1.AddDataTable1Row(drow)
        Next

I'm going to clean up the hardcoded loops, but this should do the trick.

-Jason
 
You cannot add a row to more than one datatable, and this also applies to adding it multiple times to the same table

When new, a row's .Table property is null, when added to a table, it is set to that table. From then on, unless the row is removed from the table, it cannot be added to another table, becuse the .Table property is not null
 

Latest posts

Back
Top