Most reports display some data. Typically, this data is obtained from databases. Therefore, the report has a connection to the data source in order to have a list of available tables and fields, as well as the data itself. But what if at the time of writing the report, you only had a local database that you created a connection to inside the report. Then the report will not work properly. We need a valid connection. There are two ways out of this situation: use a data source from a user program, or replace the connection line in the report.
The first method is well known to you. You need to create a connection in the application and then log in to the report. Then, when you design a report, you can choose this source of Alias. On the one hand, this is convenient - changing the connector does not affect the report. No matter where the report gets the data, as long as the table and field names, data types, and data types provided in the template are the same. The drawback of this approach is that you can't design a report without a user application. After all, the connection is only in it. Report that the developer may not have any users connected to the application. In this case, it will be appropriate to internally connect to the data in the report.
So let's look at how you can easily replace a connection to an existing report with the internal connection. Of course, the connection must be in the same database, as well as in the report. Just set another location for it.
For this application, you need to add a reference to the library: FastReport.dll and FastReport.JsonDataConnection.dll. In the catalog FastReport.net there is the installation folder ExtrasConnectionsFastReport.Json. You need to assemble the project. As a result, you get the necessary libraries. Add them to the project references. And further there is a substitution connection code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using FastReport; using FastReport.Utils; using FastReport.Data; private void Button1_Click(object sender, EventArgs e) { RegisteredObjects.AddConnection(typeof(JsonDataConnection)); Report report = new Report(); JsonDataConnection connection = new JsonDataConnection(); connection.ConnectionString = "Json=../../App_Data/nwind.json"; connection.CreateAllTables(); report.Dictionary.Connections.Add(connection); report.RegisterData(connection.DataSet); report.Load("../../App_Data/json.frx"); report.Prepare(); report.Show(); } |
In the beginning, we register the connection object to the Json data base. Next, create an instance of a report object. Create a copy of the JSON connection object. We set its ConnectionString property - basically just a link to the file. In our case, the file is located in the App_Data folder in the project. CreateAllTables function loads all of the tables from the source.
Next, we need to add a created connection to the report's connection collection and register the data source in the report.
It is important now to download the report template. If you make a connection before adding to the collection of the report connection, the trick with the substitution will not work. After downloading a template, you need to prepare a report to the display and then show it.
As a result, we have built a report, which data source was originally set up to another connection line. Thus, we can replace the connection string in any record without worrying about the database at the specified paths in the reports.