How to connect to a database with in the report

One of the first questions that arises when the developer in the study of FastReport .Net - how to connect data to the report? Fortunately, this report generator has an intuitive interface designer. However, I'm still saving your time, by talking a little about connecting  the source of the data within the report. Looking ahead, I will say that I'm going to show you how to pass the connection string from the user application. So I figuratively divided the article into two parts: connecting to a database  within a report and passing the connection string to a report.

1. There are two ways to add a new data source to a report:

• via the icon on the toolbar of the Report tab;

 

• from the Actions drop-down menu in the "Data" window.

 

Let's lookat an example. Create a data source. In the window that opens, select New connection. It is possible to choose different types of connection, up to connection to the XML database. I'll take the good old MS Access connection.

 

It is further proposed to select the table with which we work. However, you can select the data using SQL-query (not available for xml database). For this purpose there is the visual query builder. But nevertheless there's no reason why you shouldn't write request manually.

 

In the next step you can set the parameters of the query. Next - to override the data types of the columns.

So, I chose two tables: employee and orders. They can be bind by the field EmpNo. To do this, open the Action menu drop down in the "Data" and choose New relation.

 

The parent table is employee, the child table - orders. Bind on the field EmpNo.

 

Why do we do it? Now we can display a list of orders for each employee. Create a simple report template:

 

The band "Data" here contains the detail band "data." As we can see - the first one displays the name of the employee, and the second one- some order data.

The detail band "data" can be added from the context menu of the band, or by bands configuration window (Configure bands):

 

If we double click the first band "data", we will see the table employee is selected as a data source. For the second band "data” - orders. The data source is automatically selected when you add a field to the empty band. But you can change it manually.

 

If the data source is not set - the data  isnot displayed. More precisely, you will see only the first line of data for added fields. Save the report.

2. Let’s consider another interesting feature - the ability to pass the connection string to the database from the user application.

Create an application Windows Forms. Add the component Report to the form from the tab FastReport .Net and a button:

 

Passing a connection string to a report from an application.

First you need to slightly modify the previously created report. Let's open it in the designer.

Create a report parameter. We will give a connection string to it. Find the item Parameters in the "Data" window. Right-click on it - New parameter. In the Properties window, set the parameter name - conn.

 

Now select the connection in the "Data" window. Below, in the Properties window, find ConnectionStringExpression and add to it our parameter. That's all. Modification of the report is complete. Save it and return to the program.

Double-click on the button Run. In the button click event handler add the main application code:

private string ReportPath = Environment.CurrentDirectory + "/ConnectionFromApp.frx";

private string DBPath = Environment.CurrentDirectory + "/demo.mdb";

private string ConnStr;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void RunBtn_Click(object sender, EventArgs e)
 
 {
 
 using (Report report = new Report())
 
 {
 
 report.Load(ReportPath);
 
 ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath + ";User ID=Admin";
 
 report.SetParameterValue("conn", ConnStr);
 
 report.Show();
 
 }
 
 }

 Before the main action, we declare a variable for storing the path to the report and the database.

Clicking on the button creates an instance of Report. Then load the report. Assign the value of the variable of the connection string. Assign the report parameter value of the connection string. And run the report. Voila!

 

So, as it follows from the article it is clear that connecting the report to the data is a matter of minutes. And you can connect the report to almost any database. If necessary, you can transfer the connection string to the report. This come is handy, for example, when the user chooses the location of the database on a local drive.

 

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