C# Invoke Code: Exception has been thrown by the target of an invocation

Hi
I need help for one of my first robots. I use invoke code with c# as selected language, and get an Invoke Code: Exception has been thrown by the target of an invocation.

My code looks like this:

`OpgaverTilSletning=new DataTable();
OpgaverTilSletning.Columns.Add("OpgaveID");
OpgaverTilSletning.Columns.Add("Refnr");
foreach (DataRow row in AlleOpgaver.Rows)
{
    string refnr = row["Refnr"].ToString();

    // Find den matchende række i den anden DataTable
    DataRow[] matchingRows = AnalyseOpgaver.Select("Refnr = '" + refnr + "'");

    if (matchingRows.Length > 0)
    {
        // Tilføj rækken til 'OpgaverTilSletning'
        OpgaverTilSletning.Rows.Add(row);
    }
}`

Arguments:


Any idea what is wrong?

HI,

For now, can you share content of $exceptionDetails at Locals panel when error occurs in debug mode? There is detailed information for the exception.

Regards,

@Peter_E,

You have forgot to declare OpgaverTilSletning
change first like as below:
DataTable OpgaverTilSletning=new DataTable();

Thanks,
Ashok :slight_smile:

Hi Yoichi

Thanks. I get this error:
RemoteException wrapping System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> RemoteException wrapping System.ArgumentException: This row already belongs to another table.
at System.Data.DataTable.InsertRow(DataRow row, Int64 proposedID, Int32 pos, Boolean fireEvent) at UiPath.CodeRunner.UiPathCodeRunner_1daa47548f074073bdb8bb410292e83d.Run(DataTable AnalyseOpgaver, DataTable AlleOpgaver, DataTable& OpgaverTilSletning)
— End of inner exception stack trace —
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span1& 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, List1 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.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

I tried to modify line 14 to this:
OpgaverTilSletning.Rows.Add(row.Copy());
But that is not the solution.
Then i tried to add types to the columns but that didn’t make any diference too.

Thanks but unfortunately it is not that easy. If i do so, i get a validation error, because it is declared as an output argument.

Hi

The error message shows Column “OpgaveID” and/or “Refnr” already exists in the datatable.
So can you check it? If you already added them outside invokeCode, please remove these lines.
Or, add If statement to check if the above column exists, then add it when it doesn’t exist.

Regards,

With error:

Can you try this instead:

OpgaverTilSletning.Rows.Add(row.ItemArray);

For this the columns in the datatables would need to be the same which might be the case.

That worked. Thank you very much

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