Getting datatable headers as variables

I want to extract all headers from my data table as variables , so I can use these to populate a form.

I have tried using For each column and assign based on column index (0), but am getting error “Assign: Cannot create an L-value from the given expression with property ‘set_Item’ because the target object is null.”

Can anyone help me with this?

Hi. You can achieve this easily using 2 methods. LINQ + Lambda and LINQ Query:

Here’s the solution:

LINQ + Lambda:

headers = dt.Columns.Cast(Of DataColumn)().[Select](Function(x) x.ColumnName).ToArray()

LINQ Query:

(From dc In dt.Columns.Cast(Of DataColumn)() Select dc.ColumnName).ToArray()

headers = Both solutions will store the headers to an array variable.

Sorry I am not experienced on this, I do not understand how to do what you recommend

You’ll need a variable called “headers” and use assign activity.

Hi @JulieR,

Here is an alternative solution with standard UiPath activities.

Here is the suggested solution: TableHeaderArray.xaml (10.9 KB)

You can then use the ouput array and use index to look for the columns you want to assign as variables.

Here’s an example for the first method I mentioned:

image

Thank you , how can I then use that to input the data from each row from header 1 for example into a form?

Hi @JulieR, I am not quite sure if you have one row or multiple rows in your input datatable.

But in either case all you have to do is use an Assign value and then you can get the value for the column you are interested.

You can use Assign: VariableName = InputTable.Rows(0).Item(TableHeaderArray(0)).ToString

But what does that actually do?
Formula says hey you input table! Find me row index 0 and go to column = which is named = 1st item in TableHeaderArray (which here is Column1) and convert the returned value to string

But if you have a datatable which keeps updating to many rows and you are interested in the latest row value for that column then the formula is slightly different (see the image).

image

This way you can assign all your required Variables in a Multi Assign activity and thereafter use those variables to fill your form.

Hope this clears things a bit. Cheers!

Just switch your existing code from column(0).ToString --> column.ColumnName

This will access the name of the column and output it as a string as shown here: https://docs.microsoft.com/en-us/dotnet/api/system.data.datacolumn.columnname