Wednesday 4 June 2008

Displaying the IFields Collection

This handy utility was submitted by one of our users, to display the list of fields available within an InvoiceItem. The code could easily be adapted for any of the objects within Sage.

// This class is only to display the field information in a DataGrid or GridView
private class FieldNameInfo
{
private string _name;
private string _desc;
private string _typ;
private string _len;
public FieldNameInfo(string Name, string Desc, string Typ, string Len)
{
_name = Name;
_desc = Desc;
_typ = Typ;
_len = Len;
}
public string Name
{
get { return _name; }
}
public string Description
{
get { return _desc; }
}
public string Lenth
{
get { return _len; }
}
public string Typ
{
get { return _typ; }
}
}
private void checkFields()
{
lblTable.Text = "Invoice Item";
ArrayList listFields = new ArrayList();
sdo120= new SageDataObject120.SageDataObjects();
ws120 = sdo120.GetWorkSpace();
try
{
if ( ws120.Connect(ConfigurationSettings.AppSettings["SagePath"], ConfigurationSettings.AppSettings["SageUser"], ConfigurationSettings.AppSettings["SagePassword"], "ChkFields") )
{
SageDataObject120.SopItem myRec = (SageDataObject120.SopItem)ws120.CreateObject("InvoiceItem");
myRec.MoveFirst();
SageDataObject120.IFields myFields = myRec.Fields;
foreach (SageDataObject120.IField myFld in myFields)
{
FieldNameInfo fni = new FieldNameInfo(myFld.Name.ToString(), myFld.Description.ToString(), myFld.Type.ToString(), myFld.Length.ToString());
listFields.Add(fni);

}
}
}
catch(Exception ex)
{
}
finally
{
ws120.Disconnect();
}
listFieldsId.DataSource = listFields;
listFieldsId.DataBind();
}

No comments: