Copy Row from one DataTable to another

datatable
activities

#1

Barry John Edwards
May 24, 2016 12:23 ANSWERED
Hi,

I am writing an automation where two excel spreadsheet worksheets are compared. If a row in Sheet 1 is not in Sheet 2 then the row is added to sheet 3. However, I get the following exception when I try it:-

Add data row : This row already belongs to another table.

I Googled this error and it appears to be because .NET will not allow a row from one table to be copied to another. Now there are plenty of solutions out there that involve using ImportRow into the nwew table but this facility is not available in Uipath.

The question is… how do I resolve this issue in Uipath?


How to Create new data table from the existing data table?
Copy one Excel row to another Excel
Copy/Paste selected value only into new excel
#2

Andrzej.Kniola May 24, 2016 16:23
Hi,

Easiest (as in - least code) way I’m using is InvokeMethod instead of AddRow.

This is the InvokeMethod part (can’t attach xaml files, so…) :

[dt2] [row]

From a screenshot it looks like this (Parameters can be opened from InvokeMethod’s properties pane on the right):


#3

Sorry waking up old thread. Is this still valid. I try to “copy” row from another datatable to another but this Invoke method just get empty row.
I use for each row to get the row from another datatable and create new datatable with specific rows from this one bigger datatable. I get same “Add data row : This row already belongs to another table.” error if using add data row to another datatable and this Invoke method doesn’t seem to add the values from the row to the new datatable. Can somebody help?


#4

Facing the same problem here:/


#5

You can use ImportRow method to copy Row from DataTable to DataTable with the same schema

A sample reference workflow attached here:

ImportRow.xaml (9.9 KB)

Regards…!!
Aksh


I use datatable.select() return selectdatatable, Then I write selectdatatable.copytoDataTable,but
Column 'XXXX' already belongs to another DataTable
#6

Your sample can work, however, when i tried mine, it doesnt copy

Something like this

load.xaml (20.2 KB)

SAMPLE.xlsx (8.2 KB)


#7

Hey @ZacZ

Please find the attached modified workflow: load.xaml (22.3 KB)

Regards…!!
Aksh


#8

Hi @aksh1yadav I get

Is it because the output of the get row item is DateTime type?


#9

Yes and the schema and datatype problem was there as well as i mentioned schema should be same otherwise it will left that column to import


#10

I’ve tried the methods presented, but I can’t get it to work.

They copied rows are empty on the second dataTable. What should I do?


#11

Demo.xaml (13.0 KB)
You can have a try like this demo.


#12

Hello,

The issue you are having with “Add data row : This row already belongs to another table.” can be solved by passing row.ItemArray from initial row and pass it to the ArrayRow property of the Add Data Row Activity.

I would also consider the Merge data table activity which can be useful sometime.

Cheers


#14

“Add data row : This row already belongs to another table.” can be solved by passing row.ItemArray from initial row and pass it to the ArrayRow property of the Add Data Row Activity.

This worked very well for me. Excellent, thanks!


#15

let me know if you have the solution. i am also looking out for this


#16
  1. Assign DataRow[] dr=YourDatatable.select(yourcondition)
    2)use Build Datatabel Activity set output as dtO
    3)use Assign activity dtO=yourDatatable.Clone()
    4)use foreach activity and Add AddROW Activity.
    set ArrayROW in item.ItemArray()
    Datatable=dtO
    Now you have copied datarow to new datatable

#17

Work for me too ! Good method! Thank you~


#18

Hi Aksh,

What to do when two data tables have different column name. When i tried using the ImportRow method values are not getting inserted since DT’s have different column name.


#19

Hi,
When i am trying to write in to the database( i created with Build data base Activity). It is the following errorimage

and the value that i initialized for inserting is
image
where row belongs to data table one ( from which i am fetching values)
DT2 is the table in which i want to insert values.

Could you please help me in this.

Thanks in advance,
P.swathi.


#20

@pulapathiswathi, There are solutions in the same thread. Follow these links of solution from the above posts in case you missed it.

  1. Copy Row from one DataTable to another
  2. Copy Row from one DataTable to another

Regards,
Dom :slight_smile:


#21

Thanks!

but i want to use “Data Row” instead of “Array Row”.