on 12-19-2011 8:55 PM
I am trying to use the oUserFieldsMD.Remove() function but it doesn't seem to be working.
Here are the lines I am using, similar to the same as adding a UDF.
oUserFieldsMD = (SAPbobsCOM.UserFieldsMD)oComp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
oUserFieldsMD.TableName = pTableName;
oUserFieldsMD.Name = pFieldName;
nRetCode = oUserFieldsMD.Remove();
The error of the nRetCode is: No matching records found (ODBC -2028)
I have checked and the fields are there in the table. And it has worked once or twice but not consistently?
Anyone have any experience with this issue? Your help is appreciated.
Mike
Hi Mike,
If it works once or twice, have you noticed what would the difference between this and other non-working time?
Thanks,
Gordon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi try to write code like this
Create a function to check if the field exist in database like this.
private bool IsFieldExist(string tablename,string fieldname)
{
SAPbobsCOM.Recordset lRecordSet = default(SAPbobsCOM.Recordset);
try
{
lRecordSet = (SAPbobsCOM.Recordset)B1Connections.diCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
string strQry = "";
strQry = "select TableID,FieldID,AliasID from CUFD where TableID='" + tablename+ "' and AliasID='" + fieldname+ "'";
lRecordSet.DoQuery(strQry);
if (lRecordSet.RecordCount > 0)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(lRecordSet);
return true;
}
else
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(lRecordSet);
return false;
}
}
catch (Exception ex)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(lRecordSet);
return false;
}
}
2.Then write the code of field removal by doing a check with this function if the field exist or not.
Hope this will solve the problem.
Hi Mike,
In order to get the right UDF to delete you need to first use the GetByKey of the UserFieldsMD, the GetByKey has as parameteres the name of the table + the fieldID (it is not the name of the field but an index).
Here you have a sample code that shows how to proceed:
private int deleteUserField(string table, string field)
{
int lRetCode = -1;
//****************************************************************************
// In any meta-data operation there should be no other object "alive"
// but the meta-data object, otherwise the operation will fail.
// This restriction is intended to prevent a collisions
//****************************************************************************
SAPbobsCOM.UserFieldsMD oUserFieldsMD = null;
try
{
// the meta-data object needs to be initialized with a regular UserTables object
oUserFieldsMD = (SAPbobsCOM.UserFieldsMD)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
bool found = false;
int count = 0;
while (found == false)
{
if (oUserFieldsMD.GetByKey(table, count))
{
if (oUserFieldsMD.Name != field)
{
MessageBox.Show("Wrong field " + oUserFieldsMD.Name);
continue;
}
lRetCode = oUserFieldsMD.Remove();
// check for errors in the process
if (lRetCode == 0)
MessageBox.Show("Delete Field: " + oUserFieldsMD.Name + " " + oUserFieldsMD.TableName + " successful");
else
MessageBox.Show("Delete Field error: " + oCompany.GetLastErrorDescription());
}
count++;
}
}
catch (Exception ex)
{
MessageBox.Show("Delete Field failed: " + ex.Message);
}
finally
{
if (oUserFieldsMD != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oUserFieldsMD);
oUserFieldsMD = null;
}
return lRetCode;
}
Hope it helps,
Trinidad.
User | Count |
---|---|
94 | |
11 | |
11 | |
6 | |
6 | |
4 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.