logo
small logo
  • Products
  • Buy
  • Support
  • About
  • Customer panel Support
    • en
    • de
    • JP
    • ZH
  • Home
  • /
  • Articles
  • /
  • How to connect to JSON database from application code
  • Connecting to Elasticsearch

    November 10, 2021

    Now FastReport .NET, Core, Mono, and OpenSource products allow connecting to Elasticsearch. Elasticsearch is a

    read more
  • How to connect to SQLCe

    November 11, 2019

    Microsoft SQL Server Compact Edition is a simple local relational database that doesn't require installation,

    read more
  • How to connect to VistaDB

    November 15, 2019

    The Vista database will fall into the type of embedded databases. These databases differ from

    read more
  • How to connect to OracleDB from FastReport .NET

    November 15, 2019

    First of all, you can use the ODBC connector. But it is fraught with a

    read more
  • How to connect the IBM DB2 database in reports designer FastReport .NET

    November 15, 2019

    To connect the report to a DB2 database, you need to connect the plugin to

    read more

How to connect to JSON database from application code

December 15, 2019

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 an 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 database. 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. The 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 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.

about product buy
avatar
Dmitriy Fedyashov
Technical Writer
Fast Reports Team: Dmitriy Fedyashov - Technical Writer at Fast Reports
.NET FastReport JSON

Add comment
logo
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314
  • Buy
  • Download
  • Documentation
  • Testimonials
  • How to uninstall
  • FAQ
  • Tutorial Video
  • Forum
  • Articles
  • Our News
  • Press about us
  • Resellers
  • Extended licensing
  • Contact us

© 1998-2023 by Fast Reports Inc.

  • Privacy Policy