Hi,
I am getting the error:
Ref count for this object is higher than 0
when adding User-defined tables and user-defined fields programmatically with C# code. The C# code is reading from an Xml file.
The code that adds the user-defined table is as follows:
int returnCode = 0;
bool returnValue = false;
SAPbobsCOM.UserTablesMD objUserTableMD = default(SAPbobsCOM.UserTablesMD); objUserTableMD = OrderApp.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserTables);
try {
if ((!objUserTableMD.GetByKey(TableName))) {
objUserTableMD.TableName = TableName;
objUserTableMD.TableDescription = TableDescription;
objUserTableMD.TableType = TableType;
returnCode = objUserTableMD.Add();
if ((returnCode == 0)) {
returnValue = true;
}
string errorMsg;
OrderApp.oCompany.GetLastError(out returnCode, out errorMsg);
if (returnCode == 0) // SUCCESS {
Utility.WriteToLog("None", Constants.LogType.SUCCESS, "The UDT [" + TableName + "] is created successfully.", Constants.Application.SETUP);
}
else // ERROR {
throw new Exception(errorMsg);
}
} else {
string temp_string = sErrMsg;
OrderApp.oCompany.GetLastError(out returnCode, out temp_string);
if (returnCode == 0) {
Utility.WriteToLog("NA", Constants.LogType.ERROR, "The UDT: [" + TableName + "] already exists in Company.", Constants.Application.SETUP);
} else {
Utility.WriteToLog("NA", Constants.LogType.ERROR, "Error while creating UDT:" + temp_string, Constants.Application.SETUP);
}
returnValue = false; }
} catch (Exception ex) {
Utility.WriteToLog("NA", Constants.LogType.ERROR, "Error while creating UDT:" + ex.Message, Constants.Application.SETUP);
} finally {
System.Runtime.InteropServices.Marshal.ReleaseComObject(objUserTableMD); objUserTableMD = null;
GC.Collect();
}
return returnValue;
}
The return code is 1120.
I have followed the instructions here:
https://archive.sap.com/discussions/thread/1958196
i.e., making sure to dispose off the user-table object and calling GC.Collect() but still getting this error.
Also, the error happens when trying to create the first UDT so dangling user-table object should not even be a problem.
Can anyone help me fix this?
Saad
Add comment