How to store FastReport .Net reports in a database

2016-02-28

Usually reports are stored in one place but in separate files. With the increasing amount of files there are difficulties in structuring and searching. Reflecting on this subject, I came across one very interesting property of the Report object - ReportSourceString. This property holds the entire report template in a string.

Which means that it is possible to store the report template in any database. We can store all the reports in one place.

In order to save reports to the database, we need to write our own methods to save and load the report. Let's get started.

First of all, let's create a database to store reports. I have used MS Access.
The table has the following structure:

Field name

Data type

id

Counter

Name

Text

Template

MEMO

 Create a Windows Forms application. Add a connection to our database. Place the button components on the form: DataSet, BindingSource and Report.

 Looking ahead, I will say that we need a dialogue form, in which we will ask the name of the report when saving / loading:

 

For the buttons must be set DialogResult property in accordance with their name.
Getting Started Programming. We use the following FastReports libraries:

1
2
3
using FastReport;
using FastReport.Utils;
using FastReport.Design;

 And create an instance of the dialogue form:

1
public SaveLoadForm form2 = new SaveLoadForm();

 Create the event handler to save a report:

1
2
3
4
5
6
7
8
9
10
11
12
13
void cmdSave_CustomAction(object sender, EventArgs e)
 {
 ReportsDataSet.ReportsRow row;
 row = reportsDataSet1.Reports.NewReportsRow();
 if (form2.ShowDialog() == DialogResult.OK)
 {
 row.Name = form2.ReportName;
 row.Template = report1.ReportResourceString;
 this.reportsDataSet1.Reports.Rows.Add(row);
 ReportsDataSetTableAdapters.ReportsTableAdapter adapter = new ReportsDataSetTableAdapters.ReportsTableAdapter();
 adapter.Update(reportsDataSet1.Reports);
 }
 }

 Here, we create a new row in the report table. Then start the dialog form. The report name will be entered in the form. . Assign the value of the dialogue form's text field to the Name field. Write the report template as text in the Template field.

Thereafter, save the changes to the table via the adapter.

Now create an event handler of the report downloading:

1
2
3
4
5
6
7
8
9
10
11
12
13
void cmdOpen_CustomAction(object sender, EventArgs e)
 {
 if (form2.ShowDialog() == DialogResult.OK)
 {
 for (int i = 1; i < reportsDataSet1.Reports.Rows.Count; i++)
 if (reportsDataSet1.Reports[i].Name == form2.ReportName)
 {
 report1.ReportResourceString = reportsDataSet1.Reports[i].Template.ToString();
 }
 Designer designer = sender as Designer;
 designer.SetModified(this, "EditData");
 }
 }

 We also call the dialog form. In the cycle we are looking for the report with the name that  corresponds to the one entered in the text field. The report loads from Template field into ReportResourceString property. Then, the designer is updated to reflect the changes.

So, we wrote two handlers. Now it is necessary to intercept the standard event handlers and substitute our handlers.

1
2
3
4
5
private void DesignerSettings_DesignerLoaded(object sender, EventArgs e)
 {
 (sender as Designer).cmdSaveAs.CustomAction += new EventHandler(cmdSave_CustomAction);
 (sender as Designer).cmdOpen.CustomAction += new EventHandler(cmdOpen_CustomAction);
 }

As you can see, we intercept the event to save and load a report by substituting the custom handlers.

In the OnClick event of button in the main form, add the following code:

1
2
3
4
5
 private void DesignBtn_Click(object sender, EventArgs e)
 { 
 Config.DesignerSettings.DesignerLoaded += DesignerSettings_DesignerLoaded;
 report1.Design(); 
 }

 Override the handler loading the report designer.

Thus, we have created an application that allows you to save and load reports in MS Access database. You can organize the storage of reports in a desired database through property ReportSourceString report.

.NET FastReport .NET FastReport
March 25, 2025

How to Merge Multiple Reports into One in FastReport .NET

FastReport .NET is a powerful tool for creating and managing reports. In this article, we will look at how to combine multiple reports into one in FastReport .NET.
March 11, 2025

How to Use FastReport .NET Avalonia on Fedora Workstation with Wayland Protocol

In this article, we will discuss how to run FastReport .NET Avalonia on the "Fedora Workstation 39" operating system with Wayland protocol.
March 07, 2025

How to Create a QR Code with an Image in FastReport .NET

The article figured out how to insert a picture into a QR Code from the report designer FastReport.NET in just a couple of clicks.
Fast Reports
  • 800-985-8986 (English, US)
  • +31 97 01025-8466 (English, EU)
  • +49 30 56837-3928 (German, DE)
  • +55 19 98147-8148 (Portuguese, BR)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2025 Fast Reports Inc.