How to add rows from one datatable to another

I want to add rows from my temp table to master table as below:

dt_temp
ID Name
1 A
2 B

dt_master
ID Name
3 C
4 D

Desired Output(dt_master)
ID Name
1 A
2 B
3 C
4 D

@Rohit_Nandwani

Use Merge Datatable activity

Hi @Rohit_Nandwani

Use Merge DataTable activity.

Regards

Merge creates 4 columns

@Rohit_Nandwani

If your datatable Headers Must be same in two datatables then only it will merge as per your requirement.Please change manually.

What if my headers are not same?

Hi @Rohit_Nandwani

image
Output → dt_temp

image
Output-> dt_Master

Merge Data Table activity:
Source → dt_master
Destination → dt_Temp

Output:
image

Hope it helps!!

@Rohit_Nandwani

Then it will consider they are different Columns

What can be the solution to this if headers are different?

Hi @Rohit_Nandwani

If you have different columns names it will get added side by side

For Illustration

dt_Temp
image

dt_Master
image

Output:
image

Hope you understand!!
Regards

Okay, But is there a way I can get desired output with different column names?

@Rohit_Nandwani
Read Range two datatables as DT and DT1
For each Currentrow in DT1
StrID=CurrentRow(“ID1”).ToString
Name=CurrentRow(“ID1”).ToString
Add DataRow activity
Array Row:{StrID,Name}
Datatable:DT

Outside For each Row in datatable
Write range activity

Hi @Rohit_Nandwani

Check the below flow:

Below is the xaml:
Sequence3.xaml (11.4 KB)

The syntax in Add Data Row:
New Object() {Cint(CurrentRow("ID").ToString),CurrentRow("Name").ToString}

If you have any issues let me know @Rohit_Nandwani

Hope it helps!!

@Rohit_Nandwani



This is the sample process.Hope this helps for you

Please Check If your DT datatable contains More columns which DT1 not contains .In that please give " " as empty column.

I don’t want to use loop. Thing is I am reading excel in chunks where I have temp and master table. I want all rows with just one set of headers in master table.

Hi @Rohit_Nandwani

Use the below code in Invoke Code:

DtMerged = New DataTable
DtMerged.Columns.Add("ID", GetType(Int32))
DtMerged.Columns.Add("Name", GetType(String))

DtMerged = (From row In Dt1.AsEnumerable()
            Select DtMerged.LoadDataRow(New Object() {row("ID"), row("Name")}, False)).CopyToDataTable()

Dim Dt2Rows = (From row In Dt2.AsEnumerable()
               Select DtMerged.LoadDataRow(New Object() {row("ID1"), row("Name")}, False))

For Each row In Dt2Rows
    DtMerged.ImportRow(row)
Next

Invoked Code Arguments
Dt1 (Direction: In, Type: DataTable) dt_Temp
Dt2 (Direction: In, Type: DataTable) dt_Master
DtMerged (Direction: Out, Type: DataTable) Create a variable DtMerged in Varaibles Panel.

Write Range Workbook dtMerged

Let me know if you face any issues

@Rohit_Nandwani

newDataTable = (From row In DT1.AsEnumerable()
                Select newDataTable.LoadDataRow(new Object() { row("ID1").ToString(), row("ID1").ToString() }, False)).CopyToDataTable()

In the place of for each row in datatable and add datarow activity use this linq query,As compared to for loops linq queries works fastly

Hi @Rohit_Nandwani

How about the following?

dt_Result = dt_temp.Clone
dt_Result = dt_temp.AsEnumerable().
    Select(Function(r) dt_Result.LoadDataRow(New Object() {r("ID"), r("Name")}, False)).
    Concat(dt_master.AsEnumerable().
    Select(Function(r) dt_Result.LoadDataRow(New Object() {r("S.No"), r("Full Name")}, False))).
    CopyToDataTable()

Input:

dt_temp:

image

dt_master:

image

Output:

dt_Result:

image

Regards,

These solutions are too complex. I just want to preserve headers after read range without headers i.e. i have a temp table with headers but after i pass that in read range where property for Add Headers is not checked the headers of temp table changes. If I am able to preserve headers even after read range i can simply use merge.

@Rohit_Nandwani

Try using Append range workbook