Passing dataset at runtime to FastReport.Core , C#

edited February 2022 in FastReport .NET

I am using FastReport.Core 2022.1.5 nuget package in a .NET 5.0 Forms application. I have used FR Designer to generate a frx file, and I supply the data using RegisterData() at runtime. But when the Prepare is called, it throws an exception that "table is not connected to data". The data source name in frx is exactly matching the name passed to the RegisterData method.

Let me know what is missing here ?

The code flow is as below,

  FillTheDataset(); //queries using Npgsql, a postgresql db and gets back records filled in a DataSet

      Report report = new Report();

      report.Load(@"localPgReport1.frx");

      //register the dataset

      _dataSet.Tables[0].TableName = "dept";

      report.RegisterData(_dataSet,"departments");

      report.GetDataSource("departments").Enabled = true;

      //prepare the report

      report.FileName = "My Postgres Report";

      report.Prepare(); //exception in this line


FRX snippet,

<TableDataSource Name="departments" ReferenceName="departments" Enabled="true" TableName="dept">

...<DataBand Name="Data1" DataSource="departments">

.. <TableCell Name="Cell21" Text="[dept.dept_id]" Font="Arial, 10pt"/>

Comments

  • Changing the TableDataSource element inside the frx file to

    <TableDataSource Name="dept" ReferenceName="departments.dept" Enabled="true" TableName="dept"> and

    <DataBand Name="Data1" DataSource="dept">, worked fine.

  • Hello!

    To pass a DataSet to FastReport.Core at runtime, it is important that the table name / ReferenceName in the .frx file exactly matches the name in `RegisterData`, and that the data source is enabled (Enabled = true):

    using var report = new Report();
    report.Load(‘template.frx’);
    
    // Pass the DataSet with the table/link name as specified in the report’s Dictionary
    report.RegisterData(dataSet, ‘departments’);
    var ds = report.GetDataSource(‘departments’);
    if (ds != null)
    ds.Enabled = true;
    
    report.Prepare();
    

    If the table in the template was originally bound to a database connector (MSSQL/PostgreSQL), and you replace it at runtime with your own in-memory DataSet, please check that the TableDataSource.ReferenceName property (or Name/Alias) matches; otherwise, the report will attempt to connect to the old connector.

This discussion has been closed.