Invoke code add columns to datatable

Hello guys, i have a problem. I have a data table that i got from run querry activity, I need to add columns to the data table. Im trying to use invoke code, because I need to set specific data types for theese columns. The invoke code looks like this:

dt_datatable.Columns.Add("STATUS", GetType(String))
dt_datatable.Columns.Add("Status_Date", GetType(DateTime))
dt_datatable.Columns.Add("USER", GetType(String))
dt_datatable.Columns.Add("TiekejoID", GetType(Int32))

But I get an error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.   at UiPathCodeRunner_dfcd0dc2c1b74a2ca599b5b1af30e9cf.Run(DataTable dt_datatable)
	--- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
   at UiPath.Activities.System.Utilities.InvokeCode.CompilerRunner.Run(Object[] args)
   at UiPath.Activities.System.Utilities.InvokeCode.NetCodeInvoker.Run(String userCode, List`1 inArgs, IEnumerable`1 imps, Object[] args)
   at UiPath.Core.Activities.InvokeCode.Execute(CodeActivityContext context)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Hi,

Did you set instance of DataTable to dt_datatable?
Perhaps it’s necessary the follwoing expression inside or before the invoke code activity.

dt_datatable = new DataTable()

Regards,

1 Like

I did it and it kinda works…

When i try to print out dt_datatable, I cant see the added columns.

Hi,

Did you set “IN/OUT” at Direction of Argument in InvokeCode, as the following?

Regards,

Yeah, I have it like this:

HI

It’s necessary to set datatable variable at Value property.

Regards,

Oh sorry, I saw that there should be an assign activity. But if i assign dt_datatable (Which has records that I need in them) with value New DataTable(), wont the records get deleted?

Yes. it will deleted. From the above image, it’s necessary to set the datatable variable at Value property of arguments in InvokeCode activity to pass value b/w invoke code and the workflow.

Regards,

1 Like

Ok, thanks to your help, it worked. I deleted this from invoke code dt_datatable = new DataTable()

And set value in arguments of the datatable. Thanks :slight_smile:

1 Like

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