Hi, what’s up?
I need help with the following context:
I have a data table with 500,000 rows and it generates a spreadsheet in Excel. I need to generate 2 different Excel files based on the same data table.
Here’s what I did: I created a copy of the original data table and cleaned the data, leaving only the header, and then I try to pass to the empty copy the half to the end of the original data table, deleting the rows that were passed.
But every time I go to add the row to the copied data table, it returns the error “This row already belongs to another table”.
Let’s say the source datatable is dtVar. To get only the header of the source datatable, use dtVar.clone.
To get half of your source datatable, use:
dtVar.AsEnumerable.take(250000).copytodatatable
To get the remaining half, reverse your source datatable and take 250000 and reverse again.
Hi,
Just do these to take the split the data into 2:
i. the first 250000: dtOriginal.AsEnumerable.Take(250000).CopyToDataTable
ii. the last 250000: dtOriginal.AsEnumerable.Reverse.Take(250000).Reverse.CopyToDataTable
NB: You don’t need to Clone it. Remember to Check AddHeaders when you are Reading and Writing the data table.
The solution proposed by @Gbenga_Odelade will solve your problem, and is the most efficient - but for posterity’s sake here is what’s causing the “already belongs to another” error.
When you try to Add Data Row and just pass the row itself, that’s an object. It’s not going to copy the data into a new datarow object and then add it to the datatable. It’s trying to add the datarow object to the datatable. But that datarow object already belongs to a datatable.
To help clarify, instead of filling in DataRow with CurrentRow, you would put the following expression into the ArrayRow property of the Add Data Row activity:
CurrentRow.ItemArray
That way you’re passing the values of the datarow, as an array, instead of the datarow object itself.
@Gbenga_Odelade’s reply solved my problem and provided a good solution. @postwick’s explanation was very good and gave me a very good insight into other errors besides this one.