Column 'XXXX' already belongs to another DataTable

I’m trying to create a Datatable dynamically, iterating another datatable.

I have a Datatable1, which has columns a,b,c,d,e,f.
I have another datatable2, which is just instantiated as a variable but with zero columns in it.
I’m iterating this Datatable1 using it columns, like
for(eachcolumn in datatable1.columns){
having “add data column” item inside to which I’m passing datatable2 and column, so the column of datatable1 is added to datatable2, and so on for all the columns. so I don’t need to create the columns beforehand but added as per some conditions and only those columns needed for datatable2 of datatable1.
}

Throwing this error, Column ‘a’ already belongs to another DataTable.

could someone advise,

Thanks!

1 Like

The error is since you are directly adding the datacoumn object to a new datatable2.
What you should do is use the invoke method activity and invoke datatable2.columns.Add activity and pass eachColumn.ColumnName and eachColumn.Datatype as parameters to it.

1 Like

Thanks @akhi_s27, it worked.!

@vvaidya @aksh1yadav @beesheep,

After this I’m trying to add each row of datatable1 to datatable2. so the columns have data as just like datatable1.
seems like everything is empty.

Just used the same invoke method,

I didn’t add any parameters to invoke method, is this the case ?

please advise,

Thanks!

1 Like

you have to add

Regards…!!
Aksh

1 Like

But then it throws this error after adding the parameter ->> “Message: This row already belongs to another table.”

Thanks!

1 Like

you should use “ImportRow”.

and you can use if the same schema of both the datatable and other you can use Datatable.copy() asw well.

Regards…!!
Aksh

2 Likes

Thanks!

it worked but when I write this dt2 to xls, I see an empty row being added between Header row and the first row.
When I print the no of rows of dt1, it says right count figure but why is this being started with a row gap rather starting immediately after the header row.

Just attaching the workflow with the folders/package structure, so its easy to figure out just by running with minimal changes in the json config file for “filepath” attribute in the file,

Solutions.zip (7.7 KB)

1 Like

It’s all resolved guys.! there seems to be mistakenly an empty row added to the new datatable(manual mistake) and so there is an empty row gap in the csv/xls file generated after the header.

Appreciate all your efforts guys.!

Hope it’s helpful to new learners. Thanks.!

1 Like

Thanks, the Invoke method worked. By why can’t you use Add Data Row insted?

Error: This row already belongs to another table.
I thought “Add Data Row” copied the DataRow and added to datatable?

You can, but instead of passing row pass the row array.

row.ItemArray

3 Likes