You can programmatically create and insert user defined tables into Uniconta. First, define a header using the TableHeaderClient
class which defines information such as menu position, name and more. Table fields can be added using the TableField
class which holds information such as name and type. A good way to create TableFields is to use a POCO model class, and iterate over its properties. You can see this in the example below.
Example
public class TableGenerator { /* Model class to define fields in our table */ class MyModel { public DateTime Date { get; set; } public int EmployeeID { get; set; } public string PathToFile { get; set; } } private CrudAPI _crud { get; set; } public TableGenerator(CrudAPI api) { _crud = api; } public async Task < ErrorCodes > CreateTable() { var header = new TableHeaderClient { _MenuPosition = 0, // 0 = GeneralLedger, 1 = Customer etc. _Name = "MyTable", // Name of the table in database. _Prompt = "My Table", // Name of the table in client. _UserDefinedId = 1234, // Arbitraty ID you can give your table. _Attachment = true, // Records can have attachments. }; var headerResult = await _crud.Insert(header); if (headerResult != ErrorCodes.Succes) return headerResult; var type = typeof (MyModel); var fields = new List < TableField > (); foreach(PropertyInfo prop in type.GetProperties()) { var field = new TableField { _Name = prop.Name, _Prompt = prop.Name, _FieldType = GetFieldType(prop), }; field.SetMaster(header); fields.Add(field); } // Save class file to path File.WriteAllText(@ "[Path To Save MyModel.cs]", GetClassString(header, fields.ToArray())); return await _crud.Insert(fields); } /* Helper method to identify field type */ private CustomTypeCode GetFieldType(PropertyInfo prop) { CustomTypeCode type; switch (prop.PropertyType.Name.ToString()) { case "Double": type = CustomTypeCode.Double; break; case "String": type = CustomTypeCode.String; break; case "DateTime": type = CustomTypeCode.DateTime; break; case "Int32": type = CustomTypeCode.Integer; break; default: type = CustomTypeCode.Empty; break; } return type; } // Generate class string (requires ClientTools.dll) private string GetClassString(TableHeader header, TableField[] fields) { return ClassGenerator.Create(header, fields, _crud, false); }
How To Query your Use Defined Table
In order to query for your user defined table, you must acquire the generated table class.
There are multiple ways to do this.
The easiest way:
- Open Uniconta
- Tools -> User-defined tables -> Tables
- Select your table
- Click “Generate C# class”, and select the Client class.
- Copy the generated class into your code project and use it like any other table.
Programmatically (Requires ClientTools.dll):
Like shown in GetClassString method of the example code above, we can generate the class string using the ClassGenerator.Create method (from the ClientTools.dll).
For obvious reasons, it is easier to just use the easy way described above, as you only need to generate the class ones. But it is possible to generate the file programmatically too.
After getting the Table class, which looks like this for MyData:
public class MyTable : TableData { public override int UserDefinedId { get { return 1234; } } [Display(Name = "Date")] public DateTime Date { get { return this.GetUserFieldDateTime(-1); } set { this.SetUserFieldDateTime(-1, value); NotifyPropertyChanged("Date"); } } [Display(Name = "EmployeeID")] public long EmployeeID { get { return this.GetUserFieldInt64(-1); } set { this.SetUserFieldInt64(-1, value); NotifyPropertyChanged("EmployeeID"); } } [Display(Name = "PathToFile")] public string PathToFile { get { return this.GetUserFieldString(-1); } set { this.SetUserFieldString(-1, value); NotifyPropertyChanged("PathToFile"); } } }
You can simply query for records in the table as any other table:
var mytablerecords = await crud.Query<MyTable>();
IMPORTANT NOTE:
The “[Display(…)]” data annotations can be commented out. Your IDE will most likely tell you it doesn’t know the namespace.
This is fine, you can comment it out/remove it. They have no functional purpose.