Hola estoy tratando de crear una consulta que elimine campos de usuario pero al momento de dar click sale el siguiente error
“Error al eliminar campo: Error al eliminar el campo ‘1’ de la tabla ‘TABLA_PRUEBA’: -1120 - Ref count for this object is higher then 0”
pero no logro identificar el porque sale este error adjunto el codigo de lo que estoy realizando aclaro que en el que comparto quite la informacion de la creacion de tablas y campos solamente para dejarla con lo relacionado a la eliminación.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace SBO_Metadata
{
public class SBOQueries
{
private SAPbobsCOM.Recordset _recordset;
public SBOQueries()
{
_recordset = SBOClient.Instance.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
}
public List<string> GetTables()
{
List<string> values = new List<string>();
string query = @"SELECT DISTINCT ""TableName"" FROM ""OUTB"" ORDER BY ""TableName"" ASC";
_recordset.DoQuery(query);
if (_recordset.RecordCount == 0)
throw new Exception("No se obtuvieron registros de tablas");
_recordset.MoveFirst();
for (int index = 0; index < _recordset.RecordCount; index++)
{
string value = Convert.ToString(_recordset.Fields.Item("TableName").Value);
values.Add(value);
_recordset.MoveNext();
}
return values;
}
public Dictionary<string, string> ShowData(string udtName)
{
Dictionary<string, string> values = new Dictionary<string, string>();
string query = "SELECT \"TableName\", \"Descr\", \"ObjectType\" FROM OUTB WHERE \"TableName\" = '" + udtName + "'";
_recordset.DoQuery(query);
if (_recordset.RecordCount == 0)
throw new Exception("No se obtuvieron registros de tablas");
_recordset.MoveFirst();
// Obtener los valores de la consulta
string tableName = Convert.ToString(_recordset.Fields.Item("TableName").Value);
string tableDesc = Convert.ToString(_recordset.Fields.Item("Descr").Value);
string tableType = Convert.ToString(_recordset.Fields.Item("ObjectType").Value);
// Agregar los valores a la lista
values.Add("Table", tableName);
values.Add("Description", tableDesc);
values.Add("Type", tableType);
return values;
}
//
public List<string> GetFieldsForTable(string tableName)
{
List<string> fields = new List<string>();
string cufdTableName = "@" + tableName;
string query = $"SELECT \"TableID\",\"AliasID\" FROM \"CUFD\" WHERE \"TableID\" = '{cufdTableName}'";
_recordset.DoQuery(query);
if (_recordset.RecordCount == 0)
throw new Exception($"No se obtuvieron registros de campos para la tabla '{tableName}'");
_recordset.MoveFirst();
for (int index = 0; index < _recordset.RecordCount; index++)
{
string fieldName = Convert.ToString(_recordset.Fields.Item("AliasID").Value);
fields.Add(fieldName);
_recordset.MoveNext();
}
return fields;
}
public Dictionary<string, string> ShowDataFields(string fieldName, string tableName)
{
Dictionary<string, string> values = new Dictionary<string, string>();
string cufdTableName = "@" + tableName;
string query = $"SELECT \"FieldID\" FROM \"CUFD\" WHERE \"AliasID\" = '{fieldName}' AND \"TableID\" = '{cufdTableName}'";
_recordset.DoQuery(query);
if (_recordset.RecordCount == 0)
throw new Exception($"No se obtuvieron registros de Campos para '{fieldName}' en la tabla '{tableName}'");
_recordset.MoveFirst();
if (_recordset.RecordCount > 0)
{
string fieldID = Convert.ToString(_recordset.Fields.Item("FieldID").Value);
SAPbobsCOM.UserFieldsMD userFields = SBOClient.Instance.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
try
{
userFields.GetByKey(cufdTableName, int.Parse(fieldID));
values.Add("FieldID", fieldID);
values.Add("FieldName", userFields.Name);
values.Add("FieldDesc", userFields.Description);
values.Add("FieldSize", Convert.ToString(userFields.EditSize));
var fieldType = userFields.Type;
var fieldSubtype = userFields.SubType;
switch (fieldType)
{
case SAPbobsCOM.BoFieldTypes.db_Alpha:
switch (fieldSubtype)
{
case SAPbobsCOM.BoFldSubTypes.st_None:
values.Add("FieldType", "Alfanumérico");
values.Add("FieldSubtype", "Regular");
break;
case SAPbobsCOM.BoFldSubTypes.st_Address:
values.Add("FieldType", "Alfanumérico");
values.Add("FieldSubtype", "Direccion");
break;
case SAPbobsCOM.BoFldSubTypes.st_Phone:
values.Add("FieldType", "Alfanumérico");
values.Add("FieldSubtype", "Telefono");
break;
case SAPbobsCOM.BoFldSubTypes.st_Image:
values.Add("FieldType", "General");
values.Add("FieldSubtype", "Imagen");
break;
case SAPbobsCOM.BoFldSubTypes.st_Checkbox:
values.Add("FieldType", "Alfanumerico");
values.Add("FieldSubtype", "Casilla de verificación");
break;
}
break;
case SAPbobsCOM.BoFieldTypes.db_Date:
switch (fieldSubtype)
{
case SAPbobsCOM.BoFldSubTypes.st_None:
values.Add("FieldType", "Fecha/Hora");
values.Add("Fyeldtype", "Fecha");
break;
case SAPbobsCOM.BoFldSubTypes.st_Time:
values.Add("FieldType", "Fecha/Hora");
values.Add("Fyeldtype", "Hora");
break;
}
break;
case SAPbobsCOM.BoFieldTypes.db_Float:
switch (fieldSubtype)
{
case SAPbobsCOM.BoFldSubTypes.st_Rate:
values.Add("FieldType", "Unidades y totales");
values.Add("FieldSubtype", "Tasa");
break;
case SAPbobsCOM.BoFldSubTypes.st_Sum:
values.Add("FieldType", "Unidades y totales");
values.Add("FieldSubtype", "Suma");
break;
case SAPbobsCOM.BoFldSubTypes.st_Price:
values.Add("FieldType", "Unidades y totales");
values.Add("FieldSubtype", "Precio");
break;
case SAPbobsCOM.BoFldSubTypes.st_Quantity:
values.Add("FieldType", "Unidades y totales");
values.Add("FieldSubtype", "Cantidad");
break;
case SAPbobsCOM.BoFldSubTypes.st_Percentage:
values.Add("FieldType", "Unidades y totales");
values.Add("FieldSubtype", "Porcentaje");
break;
case SAPbobsCOM.BoFldSubTypes.st_Measurement:
values.Add("FieldType", "Unidades y totales");
values.Add("FieldSubtype", "Medición");
break;
}
break;
case SAPbobsCOM.BoFieldTypes.db_Memo:
values.Add("FieldType", "Alfanumérico");
values.Add("FieldSubtype", "Texto");
break;
case SAPbobsCOM.BoFieldTypes.db_Numeric:
values.Add("FieldType", "Numérico");
values.Add("FieldSubtype", "Ninguno");
break;
}
StringBuilder validValuesString = new StringBuilder();
for (int i = 0; i < userFields.ValidValues.Count; i++)
{
userFields.ValidValues.SetCurrentLine(i);
string value = userFields.ValidValues.Value;
string description = userFields.ValidValues.Description;
validValuesString.AppendLine($"{value}:{description}");
}
values.Add("FieldValidValues", validValuesString.ToString());
}
catch (Exception ex)
{
throw ex;
}
}
return values;
}
public void DeleteField(string tableName, string fieldName)
{
SAPbobsCOM.UserFieldsMD userFields = null;
try
{
string query = $"SELECT \"FieldID\" FROM \"CUFD\" WHERE \"AliasID\" = '{fieldName}' AND \"TableID\" = '@{tableName}'";
_recordset.DoQuery(query);
if (_recordset.RecordCount == 0)
{
throw new Exception($"No se encontró el campo '{fieldName}' en la tabla '{tableName}'.");
}
int fieldID = Convert.ToInt32(_recordset.Fields.Item("FieldID").Value);
Marshal.ReleaseComObject(_recordset);
_recordset = null;
GC.Collect();
userFields = SBOClient.Instance.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
userFields.GetByKey(tableName, fieldID);
int status = userFields.Remove();
if (status != 0)
{
SBOClient.Instance.Company.GetLastError(out int errCode, out string errMsg);
throw new Exception($"Error al eliminar el campo '{fieldID}' de la tabla '{tableName}': {errCode} - {errMsg}");
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (userFields != null)
{
Marshal.ReleaseComObject(userFields);
userFields = null;
GC.Collect();
}
_recordset = SBOClient.Instance.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
}
}
}
}