cancel
Showing results for 
Search instead for 
Did you mean: 

Ref count for this object is higher then 0

Former Member
0 Kudos

Hai,

I got an issue while adding user fields to the user defined table through SDK. i've created the usertableMD through SDK and the next process is to add the userfields to that table but i've encountered an issue saying "Ref count for this object is higher then 0". i'm attaching my code kindly go through n suggest me with changes.


 Private Sub adduserfields()
        Try
            Dim oufield As SAPbobsCOM.UserFieldsMD
            Dim returncode, errorcode As Integer
            Dim errordesc As String = ""
            oufield = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)
            oufield.TableName = "@TstQty"
            oufield.Name = "DocNo"
            oufield.Description = "PODocumentno"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 5
            returncode = oufield.Add()
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the Docno field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Status"
            oufield.Description = "POStatus"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Alpha
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the Status field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "ProdNo"
            oufield.Description = "POProductNo"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Alpha
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the Product No field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "PlanQty"
            oufield.Description = "POPlannedQuantity"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the Planned Quantity field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "FinishQty"
            oufield.Description = "POFinishedQuantity"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the Finished Quantity field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "RejectQty"
            oufield.Description = "PORejectedQuantity"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the Rejected Quantity field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Rjted1"
            oufield.Description = "PO1stTestRejectedQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 1st Rejected Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Avail1"
            oufield.Description = "PO1stTestAvailableQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 1st Available Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Rjted2"
            oufield.Description = "PO2ndTestRejectedQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 2nd Rejected Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Avail2"
            oufield.Description = "PO2ndTestAvailableQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 2nd Available Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Rjted3"
            oufield.Description = "PO3rdTestRejectedQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 3rd Rejected Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Avail3"
            oufield.Description = "PO3rdTestAvailableQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 3rd Available Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Rjted4"
            oufield.Description = "PO4thTestRejectedQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 4th Rejected Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Avail4"
            oufield.Description = "PO4thTestAvailableQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 4th Availble Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Rjted5"
            oufield.Description = "PO5thTestRejectedQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 5th Rejected Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Avail5"
            oufield.Description = "PO5thTestAvailableQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 5th Available Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Rjted6"
            oufield.Description = "PO6thTestRejectedQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 6th Rejected Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            oufield.TableName = "@TstQty"
            oufield.Name = "Avail6"
            oufield.Description = "PO6thTestAvailableQty"
            oufield.Type = SAPbobsCOM.BoFieldTypes.db_Numeric
            oufield.EditSize = 20
            returncode = oufield.Add
            If returncode <> 0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the 6th Available Qty field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If
            GC.Collect()
        Catch ex As Exception
            sbo_application.MessageBox(ex.Message)
        End Try
End Sub

Thanks In Advance.

Jaideep

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello ,

please asure that there is no DI-API object is "hanging around" before calling your

adduserfields()-function: Nothing like Recordsets or UserTablesMD or UserFieldsMD.

Also outside of the function!

Every DI-Object should be removed with

System.Runtime.InteropServices.Marshal.ReleaseComObject(DI-Object)

...and maybe also...

GC.Collect()

...when it isn't needed anymore.

I would also do this inside your function between every creation of a field:


               :
               :
               :
            returncode = oufield.Add
            If returncode  0 Then
                ocompany.GetLastError(errorcode, errordesc)
                sbo_application.MessageBox("Error Has Occured while creating the Status field" & vbNewLine & "Error Code: " & errorcode & " Error Desc: " & errordesc)
            End If

           '#
           System.Runtime.InteropServices.Marshal.ReleaseComObject(oufield)
           oufield = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)
           '#

            oufield.TableName = "@TstQty"
               :
               :
               :

Cheers,

Roland

Answers (2)

Answers (2)

Former Member
0 Kudos

Your answer is correct Roland but you know this errors are dependent on certain conditions, like mine i already released the objects i was using but still got error, but what i realised was that because i was using probably the same UDF & UDO as a prexexisting table i deleted, i was getting the ref. count is greater than zero. So what i did was go into the search for the existing reference to the already deleted table in OUTB and CUFD with this:=

select T0.*, t1.* from OUTB T0 inner join CUFD T1 on '@'+T0.TableName = T1.TableID

where T1.TableID = '@tableFieldName' order by T1.TableID

select T0.*, t1.* from OUTB T0 inner join CUFD T1 on '@'+T0.TableName = T1.TableID

where T1.TableID = '@TableName' order by T1.TableID

and deleted it with this query

USE [SBODemoUS]

GO

DELETE FROM OUTB

      WHERE TableName = @tableFieldName'

DELETE FROM CUFD

      WHERE TableID = '@TableName'

DELETE FROM OUTB

      WHERE TableName = '@TableName'

DELETE FROM CUFD

      WHERE TableID = '@tableFieldName'

GO

This solved my Ref. count issue.....Hope this helps someone......Thanks

Kind Regard

Oluwatosin Akinnifesi| Technical Consultant

Email: tosin@streadit.com

Website: www.streadit.com

Former Member
0 Kudos

Hi jaideep,

You need to release the UserTableMD before you instantiate the UserFieldMD object. Just calling the garbago collector won't do! Just use this code.


Try
...
Catch
     sbo_application.MessageBox(ex.Message)
Finally
     System.Runtime.InteropServices.Marshal.ReleaseComObject(oUserTableMD)
     oUserTableMD = Nothing
     GC.Collect()
End Try

Regards,

Vítor Vieira