What does ' DataTable already belongs to another DataSet.' error mean an how to fix?

mgreenberg
Contributor II

I have a data adapter that calls a BR that calls one of the IC methods (I thinned it down here) but what I run the data adapter I get the error that the datatable already belongs to another dataset.  Any help on what that means and how to resolve?

mgreenberg_0-1662560189637.png

Public Shared Function IntercompanyReportBudget(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As DashboardDataSetArgs) As DataTable
Dim Threshold As Integer = args.NameValuePairs.XFGetValue("Threshold")
Dim dtInterCoBud As New DataTable

Using dbConnApp As DbConnInfo = BRApi.Database.CreateApplicationDbConnInfo(si)
Dim interCoMethodQuery As String = "{|WFProfile|}{|WFScenario|}{|WFTime|}{}{C#USD}{V#YTD}{}{" & Threshold & "}{}{}{}{}"
dtInterCoBud = BRApi.Database.ExecuteMethodCommand(dbConnApp, XFCommandMethodTypeId.ICMatchingForWorkflowUnitMultiPlug, interCoMethodQuery, "ICBud", Nothing).Tables(0)

End Using
Return dtInterCoBud
End Function

1 ACCEPTED SOLUTION

MarcusH
Contributor III

I have just had the same error and I eventually fixed it. I am putting the solution here in case anyone else has the same error.

You need to take a copy of the table. So instead of this:

dtInterCoBud = BRApi.Database.ExecuteMethodCommand(dbConnApp, XFCommandMethodTypeId.ICMatchingForWorkflowUnitMultiPlug, interCoMethodQuery, "ICBud", Nothing).Tables(0)

add .Copy() to the end so it is like this:

dtInterCoBud = BRApi.Database.ExecuteMethodCommand(dbConnApp, XFCommandMethodTypeId.ICMatchingForWorkflowUnitMultiPlug, interCoMethodQuery, "ICBud", Nothing).Tables(0).Copy()

View solution in original post

9 REPLIES 9

NicolasArgente
Valued Contributor

Hi there!
I think the error could come from 2 places.

Can you rename your Function to IntercompanyReportBudget2 ?
Your other dt to dtInterCoBud2 ?



Public Shared Function IntercompanyReportBudget2(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As DashboardDataSetArgs) As DataTable
Dim Threshold As Integer = args.NameValuePairs.XFGetValue("Threshold")
Dim dtInterCoBud2 As New DataTable

Using dbConnApp As DbConnInfo = BRApi.Database.CreateApplicationDbConnInfo(si)
Dim interCoMethodQuery As String = "{|WFProfile|}{|WFScenario|}{|WFTime|}{}{C#USD}{V#YTD}{}{" & Threshold & "}{}{}{}{}"
dtInterCoBud2 = BRApi.Database.ExecuteMethodCommand(dbConnApp, XFCommandMethodTypeId.ICMatchingForWorkflowUnitMultiPlug, interCoMethodQuery, "ICBud", Nothing).Tables(0)

End Using
Return dtInterCoBud2
End Function
Connect with me on:
LinkedIn: https://www.linkedin.com/in/nicolas-argente/
Website: https://aiqos.io
If you want to lift yourself up, lift up someone else.

So I stole some code from another BR we had that was putting the method call into a temp table and then copying it to the table that got returned and now I am not getting the error anymore. So Strange - not sure why it needs to be that way

 

Dim tempTable As DataTable = BRApi.Database.ExecuteMethodCommand(dbConnApp, XFCommandMethodTypeId.ICMatchingForWorkflowUnitMultiPlug, interCoMethodQuery, "ICBud", Nothing).Tables(0)
If (dtInterCoBud2 Is Nothing) Then
dtInterCoBud2 = tempTable.Copy
Else
dtInterCoBud2.Merge(tempTable)
End If

Could you try changing your original code from

Dim dtInterCoBud As New DataTable

to

Dim dtInterCoBud As DataTable

Hi Daniel - same error 😂

Could the error come from the method query you use in the BR? Are you using dt or ds there?

Connect with me on:
LinkedIn: https://www.linkedin.com/in/nicolas-argente/
Website: https://aiqos.io
If you want to lift yourself up, lift up someone else.

Hi Nicolas - I dont know what you mean by dt or ds.   Can you please elaborate? 

Sorry 🙂 I meant Datatable. DataSet. There must be one somewhere that is used in the script but has the same name twice. Not sure... but i would check that 🙂

Connect with me on:
LinkedIn: https://www.linkedin.com/in/nicolas-argente/
Website: https://aiqos.io
If you want to lift yourself up, lift up someone else.

MarcusH
Contributor III

I have just had the same error and I eventually fixed it. I am putting the solution here in case anyone else has the same error.

You need to take a copy of the table. So instead of this:

dtInterCoBud = BRApi.Database.ExecuteMethodCommand(dbConnApp, XFCommandMethodTypeId.ICMatchingForWorkflowUnitMultiPlug, interCoMethodQuery, "ICBud", Nothing).Tables(0)

add .Copy() to the end so it is like this:

dtInterCoBud = BRApi.Database.ExecuteMethodCommand(dbConnApp, XFCommandMethodTypeId.ICMatchingForWorkflowUnitMultiPlug, interCoMethodQuery, "ICBud", Nothing).Tables(0).Copy()

Kamila
Contributor

Thanks Marcus, it was really helpful!