How to use data source in FastReport Open Source reports

When you create a web report for FastReport Open Source, you use a database to construct a template.

You create an internal connection to the data source and add table fields to the report page. But, when the report is used in real conditions, the database specified in the internal connection may not be available. And then you will see such kind of error.


Resetting the connection string to the data source in the report will help out of this situation.

Of course, the connection string must be in the same database that was used when creating the report template. After all, the template contains specific names of tables and fields.

You can reset the connection string in the report in two ways:
• Directly from user application code;
• Through the report parameter.

Let's look at both ways in more details.

Let's create a report template with an internal connection to the data source using the free report designer FastReport Designer Community. No matter what the report is about, we are interested in the data connection.


Add this report to your ASP.NET Core web application with FastReport Open Source. In this example, the XML database is used as the data source. For the experiment, change the name of this xml file. After that, the report will stop working in preview mode, and when you try to build it in a web application, we will see an error, shown at the beginning of the article.

Now let's change the code for web report generation:

public WebReport GetReport()
        {
            string webRootPath = _hostingEnvironment.WebRootPath; // Get the path to the wwwroot folder
            WebReport webReport = new WebReport(); // Create a Web Report Object
            webReport.Report.Load(webRootPath + "/reports/Master-Detail.frx"); // Load the report into the WebReport object
            webReport.Report.Dictionary.Connections[0].ConnectionString = "XsdFile=;XmlFile=C:\\Users\\Dimon\\source\\repos\\FROS_MVC_PDF\\FROS_MVC_PDF\\wwwroot\\reports\\nwind1.xml";
            return webReport;
        }

As you can see, we just changed the connection string in the data source. After this report is generated without any errors:


Now consider the second method of substitution of the connection string. This time the substitution will occur within the report. Actually, it is not even a substitution, but the installation of a connection string, because we will use the parameter instead of the real value.

In the report we created earlier, add a parameter called ConnectionString:


Then, select ‘Connection’ with the mouse. In the Property Inspector, find ConnectionStringExpression.

Let's choose the created ConnectionString variable for it:


Now back to our application. Let's change the web report generation method:

        public WebReport GetReport()
        {
            string webRootPath = _hostingEnvironment.WebRootPath; // Get the path to the wwwroot folder
            WebReport webReport = new WebReport(); // Create a Web Report Object
            webReport.Report.Load(webRootPath + "/reports/Master-Detail.frx"); // Load the report into the WebReport object
            var dataSet = new DataSet(); // Create a data source
          
            dataSet.ReadXml(webRootPath + "/reports/nwind1.xml");

            webReport.Report.SetParameterValue("ConnectionString", "XsdFile=;XmlFile=C:\\Users\\Dimon\\source\\repos\\FROS_MVC_PDF\\FROS_MVC_PDF\\wwwroot\\reports\\nwind1.xml");
            return webReport;
        }
As a result, we get a workable report.

What is the difference between these approaches? In the first case, we have a report with the specified connection string, and in the second, with an undefined one. In our case, there is no difference. We can easily set the connection string or report parameter directly from the application code.

Comments

  1. I created a report using SQL server, but when executing this line:
    webReport.Report.Load(webRootPath + "/reports/indicator11.frx");

    I have this error when loading the report:
    FastReport.Utils.ClassException: 'Can't find object MsSqlDataConnection'


    NOTE: I created the report with fast report community designer.

    ReplyDelete
    Replies
    1. In Package Manager Console:
      Install-Package FastReport.OpenSource.Data.MsSql

      In Startup.cs:
      FastReport.Utils.RegisteredObjects.AddConnection(typeof(FastReport.Data.MsSqlDataConnection));

      Delete
    2. not mentioned in the article , thank you very much.

      Delete
  2. I have created a fast report with parameter and based on that parameter i want to get the report data.
    i build report with query like " select * from Bill inner join BillDetail on Bill.id = BillDetail.BillId
    where Bill.id = " + @ID. Here @ID is Parameter. when i call this report from dot net core i set param value from code but the data is not fetch based on the parameter which i passed. Some one help me please.

    ReplyDelete
  3. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own Blog Engine blog now. Really the blogging is spreading its wings rapidly. Your article is a fine example of it.
    Data science classes in pune
    Data science course in pune
    Data science training in pune

    ReplyDelete
  4. Your blog had very good knowledge and that gave huge instructions and that was really commendable ideas. you have provided good knowledge on this topic please share more information with us.

    SQL Server Load Rest API

    ReplyDelete

Post a Comment

Popular posts

How to use FastReport Open Source in ASP.NET Core Web API application

FastReport Open Source - what is it and how to use it

FastReport Designer Community Edition