Search Results for

    Show / Hide Table of Contents

    Passing own connection string

    If you use data sources that are defined inside a report, you may need to pass an application-defined connection string to a report. This can be done in three ways.

    The first method: you pass a connection string directly to the Connection object in a report. Do the following:

    report1.Load(...); 
    // do it after loading the report, before running it
    // assume we have one connection in the report
    report1.Dictionary.Connections[0].ConnectionString = my_connection_string;
    report1.Show();
    

    The second method: you pass a connection string using the report parameter. Do the following:

    • run the report designer;
    • in the "Data" window, create a new report parameter (with "MyParameter" name, for example). See the User's Manual for more details;
    • in the "Data" window, select the "Connection" object that contains a data source;
    • switch to the "Properties" window and set the ConnectionStringExpression property to the following:
    [MyParameter]
    
    • pass the connection string to the MyParameter parameter:
    report1.SetParameterValue("MyParameter", my_connection_string);
    

    The third method: use the DatabaseLogin event of the FastReport.Utils.Config.ReportSettings global class (see the "Configuring the FastReport environment" section). This event occurs each time when FastReport opens the connection. Here is an example of this event handler:

    private void environmentSettings1_DatabaseLogin(
      object sender, DatabaseLoginEventArgs e)
    {
      e.ConnectionString = my_connection_string;
    }
    

    Keep in mind that the DatabaseLogin event is global, it works with all reports.

    Back to top © 1998-2025 Copyright Fast Reports Inc.