Skip to content

Set Connection String Runtime

Peter Gill edited this page May 3, 2024 · 12 revisions
dotnet add package Majorsilence.Reporting.RdlViewer

Using a dynamic connection string in My-FyiReporting is easy and simple but it does require a few steps.

The following will describe the steps necessary to dynamically set the connection strings. Examine the screenshots below for more details.

Basic Instructions

These instructions will add a new report parameter called ConnectionString that will be set in c# or vb.net code.

  • Open the report in the designer
  • In the properties panel make sure "Report" is selected
  • Find the "Parameters" and click the "..." load button
  • In the parameter window that opens click "Add"
    • Enter a name
    • If you want to prompt the user with the builtin prompt enter a prompt
    • Set the datatype to "String"
    • Click OK
  • Back in the main designer go to the menu Data -> Data Sources
    • Select the data source whose connection should be set by the parameter
    • Either clear out the connection string and type in the parameter or
      • Click the "fx" button
      • Clear the connection string
      • Find the Parameter under the available parameter and add it by selecting it and clicking the ">>" button
      • Click OK
      • If satisfied with the connection string click OK

C-sharp app Connection String

using System.Windows.Forms;
using fyiReporting;
using fyiReporting.RDL;
using fyiReporting.RdlViewer;

class Program
{
    [STAThread]
    static void Main()
    {
        System.Windows.Forms.Form frm = new System.Windows.Forms.Form();
        frm.Height = 600;
        frm.Width = 800;

        fyiReporting.RdlViewer.RdlViewer rdlView = new fyiReporting.RdlViewer.RdlViewer();
        rdlView.SourceFile = new Uri(@"\path\to\your\report_with_dynamic_connection_string.rdl");
        rdlView.Parameters += string.Format("ConnectionString={0}", "The Connection String Goes Here");
        rdlView.Rebuild();

        rdlView.Dock = DockStyle.Fill;
        frm.Controls.Add(rdlView);

        Application.Run(frm);
    }
}

Visual Instructions

The first thing that needs to be done is open the report in the designer. In the properties panel make sure "Report" is selected. Find the "Parameters" and click the "..." load button

My-FyiReporting Connection String Runtime Image 1

In the parameter window that opens click "Add". Enter a name. If you want to prompt the user with the builtin prompt enter a prompt. Set the datatype to "String". Click OK.

My-FyiReporting Connection String Runtime Image 2

Back in the main designer go to the menu Data -> Data Sources

My-FyiReporting Connection String Runtime Image 3

Select the data source whose connection should be set by the parameter. Either clear out the connection string and type in the parameter. Or you can Click the "fx" button.
My-FyiReporting Connection String Runtime Image 4

If you clicked the "fx" button clear the connection string, and find the Parameter under the available parameter and add it by selecting it and clicking the ">>" button. Click OK.

My-FyiReporting Connection String Runtime Image 5

If satisfied with the connection string click OK.

My-FyiReporting Connection String Runtime Image 6

The next two screenshot shows a (sample application)[https://github.com/majorsilence/My-FyiReporting/tree/master/Examples/SampleApp] that lets the user enter the connection string.

The first screenshot shows connecting to the sqlitetestdb2.db database and its data.

My-FyiReporting Connection String Runtime Image 7

The second screenshot shows the same report connecting to the northwindEF.db and shows that it has different data.

My-FyiReporting Connection String Runtime Image 8