Error: Ref count for this object is higher then 0
I am trying to create User Defined Tables in SAP through Coding but while running the code the error "Ref count for this object is higher then 0 " is showing .
I have created a temporary table in which the Table Description are stored as follows :-
Create Table Test_Table (TblCode Varchar(30),TblName Varchar(100),TblType int)
From this table then using recordset i am getting the table details for creation :-
oRecord.DoQuery("Select TblCode,TblName,TblType From Csv_UDT")
While Not oRecord.EoF
Dim Table_Id As String = oRecord.Fields.Item("TblCode").Value
Dim Table_Name As String = oRecord.Fields.Item("TblName").Value
Dim Table_Type As integer = oRecord.Fields.Item("TblType").Value
Dim oUserTablesMD As SAPbobsCOM.UserTablesMD
oUserTablesMD = Class_OprTbl_Main.ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserTables)
'// set the table parameters
oUserTablesMD.TableName = Table_Id
oUserTablesMD.TableDescription = Table_Name
oUserTablesMD.TableType = Table_Type
lRetCode = oUserTablesMD.Add
If lRetCode <> 0 Then
If lRetCode = -1 Then
Class_OprTbl_Main.SBO_Application.MessageBox(sErrMsg + oUserTablesMD.TableName)
oUserTablesMD = Nothing
But while running this code the error is coming .
How to solve this error ........
First of all, you are not meant to create temporary table in SAP database. SAP have never made an official announcement, but I think they would not be too happy if they saw it and may withdraw support. Just something to be aware of.
Secondly, your error is very common and you would have found the solution by searching the forum. It is caused by having an open recordset while trying to use a metadata object.
You will have to change how your code works. You can not use the UserFieldsMD or UserTablesMD object while you have an undisposed recordset object.
The solution is you must dispose of any recordset object before attempting to use the UserTablesMD object:
System.Runtime.InteropServices.Marshal.ReleaseComObject(oRecordSet) oRecordSet= Nothing GC.Collect() GC.WaitForPendingFinalizers()