How to make auto creation of aliases for database field

Sometimes with some data sources we have to create aliases for the table fields. This happens when the field names are not understood intuitively or their native language is not used in their naming. For the convenience of working with such a database, FastReport .Net allows you to create aliases. But it is inconvenient to create them manually since every time you have to create a new report. So I'll show you how to create aliases in the code. This allows you to use this data source in multiple reports.

To demonstrate it, I'll create an application with German localization. That is, the interface and the field names are in German.

Keep in mind that data source with aliases should be accessible when you create a report using the File-> New menu. In order to do this, we need listen for the event of selection menu New item.

Let's use a Windows Forms application.

Add two buttons to the form.

 

The first button launches the designer with a blank report. Second - opens a demo account in the designer.
Add the libraries:

1
2
3
4
5
6
using FastReport;
using FastReport.Utils;
using FastReport.Data;
using FastReport.Design;
using FastReport.Wizards;
using System.IO;

 First of all, create a procedure for registering a data source for the report:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 private void RegisterData(Report report)
 {
 // cteate any DataSet
 DataSet dataSet = new DataSet();
 dataSet.ReadXml(Path.Combine(appPath, "nwind.xml"));
 
 // register data source in the report
 report.RegisterData(dataSet, "NorthWind");
 
 // Loop through the tables. Override aliases have necessary tables. Activate visibility of the tables
 foreach (DataSourceBase dsItem in report.Dictionary.DataSources)
 {
 if (dsItem.Name == "Employees")
 {
 dsItem.Enabled = true;
 dsItem.Alias = "Mitarbeiter";
 dsItem.Columns.FindByName("EmployeeID").Alias = "Identifier";
 dsItem.Columns.FindByName("LastName").Alias = "Nachname";
 dsItem.Columns.FindByName("FirstName").Alias = "Vorname";
 dsItem.Columns.FindByName("Title").Alias = "City";
 dsItem.Columns.FindByName("TitleOfCourtesy").Alias = "Titel";
 dsItem.Columns.FindByName("BirthDate").Alias = "Geburtsdatum";
 dsItem.Columns.FindByName("HireDate").Alias = "Datum der Beschäftigung";
 dsItem.Columns.FindByName("Address").Alias = "Anschrift";
 dsItem.Columns.FindByName("City").Alias = "Stadt";
 dsItem.Columns.FindByName("Region").Alias = "Bereich";
 dsItem.Columns.FindByName("PostalCode").Alias = "Index";
 dsItem.Columns.FindByName("Country").Alias = "Land";
 dsItem.Columns.FindByName("HomePhone").Alias = "Haustelefon";
 dsItem.Columns.FindByName("Extension").Alias = "Area Code";
 dsItem.Columns.FindByName("Photo").Alias = "Photographie";
 dsItem.Columns.FindByName("Notes").Alias = "Hinweise";
 dsItem.Columns.FindByName("ReportsTo").Alias = "Vorlage";
 }
 }
 
 // set report parameters
 report.SetParameterValue("Die Testparameter 1", "Der Parameter 1 vor dem Aufruf des Berichts");
 report.SetParameterValue("Parameter 2", "\"Der Parameter 2 Standard\"");
 }

 As you can see, we have created a DataSet. Then, we loaded XML database into it. Then, registered the data source in the report. In the cycle in the data source we find the “Employees “ table. Enable it (Enabled = true). We made it for the table to appear in the designer’s data window. Now we assign an alias to each field of the table - the name of the German language.

Handling the designer load event:

1
2
3
4
5
6
7
8
9
10
private string appPath;
 
private void Designer_Load(object sender, EventArgs e)
 { 
 appPath = Path.GetDirectoryName(Application.ExecutablePath);
 // load the German local for designer
 Res.LoadLocale(Path.Combine(appPath, "German.frl"));
 // intercept the load event of designer for further overriding the event handlers
 Config.DesignerSettings.DesignerLoaded += DesignerSettings_DesignerLoaded;
 }

 Set localization language in the designer resources - in this case German.

Then assign the event handler DesignerLoaded to our own event, which we write below.

Write a handler for DesignerLoaded:

1
2
3
4
5
6
7
8
9
 // load event of designer
 private void DesignerSettings_DesignerLoaded(object sender, EventArgs e)
 {
 // intercept action of creating a new report for the registration of our sources and parameters
 (sender as Designer).cmdNew.CustomAction += new EventHandler(cmdNew_CustomAction);
 
 // except cmdNew. Similarly, you can intercept other Designer.cmd * and do anything inside,
 // for example, load report from database field and save it back
 }

 Here, we assign the event of New menu selection our handler. This is a way of intercepting the event.

We write the handler to select the menu item New:

1
2
3
4
5
6
7
8
9
10
11
12
13
 void cmdNew_CustomAction(object sender, EventArgs e)
 {
 Designer designer = sender as Designer;
 // Use blank wizard to create blank report
 BlankReportWizard wizard = new BlankReportWizard();
 //StandardReportWizard wizard = new StandardReportWizard(); // you can use other wizards
 // run wizard
 wizard.Run(designer);
 // register our data and parameters
 RegisterData(designer.Report);
 // update data tree
 designer.SetModified(this, "EditData");
 }

Here we use a blank designer. Create an instance of Blank Report Wizard. As the name implies – it’s a blank report. Then we run the designer, registering a data source and update the data tree in the data window.
It's time to create handlers for the two buttons. The button of creation a new report:     

1
2
3
4
5
6
7
8
9
 // create new report
 private void btnNewReport_Click(object sender, EventArgs e)
 {
 using (Report report = new Report())
 {
 RegisterData(report); 
 report.Design();
 }
 }

 Here, the report object is created. The data is recorded for it. Run the designer.

The button of report editing. Basically, it’s the same thing, but add the loading of existing report:    

1
2
3
4
5
6
7
8
9
10
 // edit report
 private void btnLoadReport_Click(object sender, EventArgs e)
 {
 using (Report report = new Report())
 {
 report.Load(Path.Combine(appPath, "report.frx"));
 RegisterData(report);
 report.Design();
 }
 }

 Do not forget that in the folder with the executable file there should be: the report, the database file and the locale (German.frl).
Run the application. Click on the second button. We get the report template. Let us open a data source in the tree:

 

All fields, as well as the table name, are displayed in German, thanks to aliases.
Now, create a new blank report using the File-> New menu.
Once again, we see our data source alias.

We have created a data source and assigned aliases to the table fields. Now we can use this source in this form in any report.

By using aliases it is much easier to create reports for people who are not familiar with the field names in a particular database. You can use aliases only once in the database, and get rid of the many questions from the report developers.

And lastly the report itself:

 

Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314

© 1998-2024 Fast Reports Inc.
Trustpilot