Clear Datatable clears a copy of the Data Table too

Scenario:

While clearing a copy of a datatable, the original and the copy gets cleared.

Steps to reproduce:

  1. Initialize a datatable (say DT1) with some content (I used a Read Range activity to read from excel and store in datatable)
  2. Assign DT2 = DT1
  3. Clear Data Table DT2

Current Behavior:

Both DT1 and DT2 become null

Expected Behavior:

Only DT2 should become null

Studio/Robot/Orchestrator Version:

Last stable behavior: NA
Last stable version: NA
OS Version: Windows 7
Others if Relevant: (workflow, logs, .net version, service pack, etc):
UiPath.System.Activities v19.3.0

1 Like

I believe this is an issue of the assignment being by reference instead of by value. When you’re doing your assignment DT2 = DT1, you’re not doing an actual copy of the data. Both variables at that point, DT2 and DT1 are pointing to the same DataTable. So any changes done with one will be reflected in calls to the other.

Please see this documentation for how to do a copy: DataTable.Copy Method (System.Data) | Microsoft Docs

3 Likes

Yeah, he is correct. When you do DT2 = DT1, they are both the same data table.

Here’s a solution, which I use whenever I need a new table with the same data:
DT2 = DT1.AsEnumerable.CopyToDatatable

1 Like

Thanks @dmccammond and @ClaytonM. Problem solved now :slight_smile:
Both the ideas work!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.