Copy DataTable to another DataTable

Hi I have an array of DataTable and I would like to exact copy my DataTable created into the array. Is it possible?

If you want to copy a datatable:

myNewDT = DT.Copy()

If you want to create a datatable from an array of datarows:

myNewDT = myArray.CopyToDataTable()

4 Likes

Assigning a variable with type System.Data.DataRow() (a datarow array) to DtData.Select will create an array of rows from datatable DtData.

Tried the DT.Copy() it throws this error instead

Could you please try before your assignment:

Assign (DataTable)
dtTemp = dtLineItem.Copy()

?

1 Like

Seems like there is an issue when assigning datatable to an array of datatable. When I tried to assign to a single datatable it’s fine

And

Log Message
dtArray.Length.ToString

?

This error appears when I tried to assign a dt to an array of dt

the message states that your index is beyond the array limit. Are your trying to build an array or to update an already existing member of the array?

I’m trying to build an array. I declared the array as variables and later on I assigned it with my datatable

You could build a List with default as New List(Of DataTable), add your datatables to the list then convert it later to an Array if necessary.

I’m convinced the problem is with your array’s initialisation.

EDIT: I’m discovering VB with UiPath and just find out about DataSet: might be a better structure. I’ll dig into it tonight.

2 Likes

Alright thank you so much for your help @msan

@Seungwan

Hello,

While DataSet is very neat, it might not suit your approach.

If you know the number of item your array will have, you might do:

Assign (Int32)
intArrayLength = ?

Assign (Array Of DataTable)
dtArray = New DataTable(intArrayLength){}

Your array will be ready for your loop.

If you don’t know how many items you’ll have, create first a list, add each item in your loop then if necessary convert the list to an array:

Assign (List Of DataTable)
dtList = New List(Of DataTable)

Loop

  • (whatever you need)…

  • AddToCollection
    Type of argument: DataTable
    Collection: dtList
    Item: dtLineItem.Copy()

  • (whatever you need)…

// If needed
Assign (Array Of DataTable)
dtArray = dtList.ToArray

1 Like

Hi @Seungwan !

To make an exact copy of a DataTable into an array of DataTable in UiPath, you can use the following steps:

  1. Declare an array of DataTable with the desired size:

    • Use the Assign activity to create an array variable of DataTable type with the desired size. For example:
    ArrayVar = New DataTable[ArraySize]
    
  2. Copy the DataTable into the array:

    • Use the Assign activity to copy the DataTable into the array at the desired index. For example, if you want to copy SourceDataTable into index 0 of the array:
    ArrayVar(0) = SourceDataTable.Clone()
    ArrayVar(0).Merge(SourceDataTable)
    
  3. Repeat the above steps for each DataTable you want to copy into the array, adjusting the index accordingly.

By following these steps, you can create an exact copy of a DataTable and store it in an array of DataTable in UiPath.