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()

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()

?

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