FastReport.Core / sqlConnection

Hey,

using the trial version I tried the following code:
            
var sqlConnection = new MsSqlDataConnection();
sqlConnection.ConnectionString = _connectionString;
           sqlConnection.CreateAllTables();
            report.Report.Dictionary.Connections.Add(sqlConnection);
            report.Report.Load(reportFile);

This example throws the following exception on Load(reportfile): FastReport.Utils.ClassException: 'Can't find object MsSqlDataConnection'

Using a Report without Database connection works as expectd.

Any Idea on how to use a database connection on .core?

Thanks
Matthias

Comments

  • Hello!

    In your code snippet, a connection is first added to `report.Report.Dictionary.Connections`, and then `report.Report.Load(reportFile)` is called. The `Load` method completely clears and overwrites the report (including the `Dictionary`) with the contents of the .frx file.

    Recommended procedure:

    // 1. Register the connection type (only needs to be done once when the application starts)
    RegisteredObjects.AddConnection(typeof(MsSqlDataConnection));
    
    // 2. Load the .frx template
    report.Report.Load(reportFile);
    
    // 3. Configure the connection from the loaded dictionary (or add a new one after Load)
    if (report.Report.Dictionary.Connections.Count > 0 &&
    report.Report.Dictionary.Connections[0] is MsSqlDataConnection conn)
    {
    conn.ConnectionString = _connectionString;
    }
    else
    {
    var sqlConnection = new MsSqlDataConnection
    {
    ConnectionString = _connectionString
    };
    sqlConnection.CreateAllTables();
    report.Report.Dictionary.Connections.Add(sqlConnection);
    }
    

    Also, make sure that the FastReport.Data.MsSql package (corresponding to your version of FastReport.Core) is installed in the project and that the connection string is set correctly for the target database.

This discussion has been closed.