Skip to Content

1120: Ref count for this object is higher than 0 error when adding UDT's and UDF's

Jun 16, 2017 at 08:55 PM


avatar image


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;



return returnValue;


The return code is 1120.

I have followed the instructions here:

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?


10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

DIEGO LOTHER Jun 16, 2017 at 10:54 PM

Hi Saad,

Your code seems correct. Try run this code isolated. Create a sample project, connect to the company object and then run your statements. If run ok, probably you do perform some action before this piece of code that keep for example a recordset object opened.

Hope it helps.

Kind Regards,

Diego Lother

10 |10000 characters needed characters left characters exceeded