Using vb.net code to convert dictionary to datatable

Hi,

can someone please explain how can I use vb.net code into a code editor activity in uipath, in order to convert dictionary to datatable?

thanks in advance

Hi.

Well you can use the Invoke Code activity if you want to use a code editor.

I don’t think you really need to use coding, though, because there are activities that can accomplish.

First, you need a datatable initialized.
—use Build Data Table activity. Calculate what columns you need from the dictionary (ie dict.Keys). Add Data Column as needed, possibly using a loop
—loop through the values and use Add Data Row to add the value from the dictionary

On the other hand,
If your dictionary came from an Excel File, then go back and get the data table from the Excel File using Excel Read Range.

So, those are just some ideas.

If you really do want to use .net coding with the Invoke Code activity, then I suggest doing some searches online for the syntax, and maybe someone already made a function to convert dictionary to data table. - this, I am not sure of.

Regards.

2 Likes

Hi,

You can try using activity as ClyatonM said, which is the best way to maintain and visually better.

Or you can use InvokeCode activity, and since I don’t know if you already know the code you’d use, here’s a piece of code that may help you:

dt = New DataTable("dt")

Dim keyColumn As DataColumn = New DataColumn("Key")
keyColumn.DataType = System.Type.GetType("System.String")
Dim valueColumn As DataColumn = New DataColumn("Value")
valueColumn.DataType = System.Type.GetType("System.String") 

dt.Columns.Add(keyColumn)
dt.Columns.Add(valueColumn)

For Each item As KeyValuePair(Of String, String) In dic
    dt.Rows.Add(item.Key, item.Value)
Next
1 Like

Thank you so much for your help!

Thanks, very helpful!