Add DataColumn and copy 3 columns to it

Hi all,
*I’m trying to add a data column with the invoke method so that the new Column is added before a specific column. I’m getting an error.
*I’d like to assign 3 columns to the new column, wrote something like this:
dt.Columns(“NewColumn”).Expression=“Trim([FirstName] + ’ ’ + TRIM[LastName] + ’ ’ + TRIM[ID])”
Is there a better way?

Any help would be highly appreciated.
Thank you!

Hi @hp321 ,

I believe the method name should be SetOrdinal, there is a typo mistake made in the Method name used.

1 Like

@hp321

you can also try with invoke code activity

dt1.Columns.Add.SetOrdinal(3)

inplace of 3 you can pass the integer value whereever the column you want toadd

Please share with us the details:

  • usage of the TargetObject Setting
  • Parameters

ensure that the Parameters are correctly configured and the position is provided

You’re right @supermanPunch
Thanks. That got rid of the error in invoke method.

1 Like

@Shiva_Nikhil ,can you please specify the steps before and after the invoke code activity?
I’m not familiar with it…Do I need to add datacolumn before? How to specify the names of columns to be added? etc
Thanks

@hp321

dt1.Columns.Add("ColumnName").SetOrdinal(dt1.Columns.Count-1)

or

dt1.Columns.Add("ColumnName").SetOrdinal(3)

it will add the datacolumn with the name , just replace ColumnName with the name which ever you want

No need to use add data column to add the column

Thank you so much @Shiva_Nikhil :grinning:

Now I have the new column and I want to copy 3 columns with a space.
I assigned the below, but getting an error:
dt.Columns(“NewColumn”).Expression=“Trim([FirstName] + ’ ’ + TRIM[LastName] + ’ ’ + TRIM[ID])”
Anyone has any ideas?
Thanks

dt.Columns(“NewColumn”).Expression=“Trim([FirstName]) + ’ ’ + TRIM([LastName]) + ’ ’ + TRIM([ID])”

can you try this once

Getting this error:
image

@hp321

type the expression manully , Please dont copy and paste

Its working fine

Thanks @Shiva_Nikhil
The columns were copied as expected, though the header now contains the expression[before combining the columns the header was ok]:
image

Anyone have an idea how to resolve this?
image

I created a new column “Client and Payor”. wrote nicely in Excel.
Once I combined a few columns with as assign, the expression was added to the header

Hi @hp321 ,

Maybe one of the ways to tackle it is using a different/new Datatable variable and assigning the value to it like below :

OutputDT = DT.AsEnumerable.CopyToDatatable

From Debug :

1 Like

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