ActiveReports 18 .NET Edition
Report Authors: Designer Components / Design Reports / Design Section Reports / Interactivity / Parameters
In This Topic
    Parameters
    In This Topic

    In a Section report, you can use the Parameters collection to pass values directly into a control, or you can also use it to display a subset of data in a particular instance of the report.

    There are several ways to set up parameters in a report:

    Add Parameters via the SQL Query

    When you add a single parameter to a report's Parameters collection via the SQL query, the query looks like this:

    SELECT * FROM Products
    INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
    WHERE Products.SupplierID = <%SupplierID|Enter Supplier ID|1|S|True|%>

    You can also create a parameterized query from the Visual Query Designer. See Query Builder in Microsoft SQL Client and OLEDB Providers for further information on how to create a parameterized query using the interactive query designer.

    There are six values in the parameter syntax, separated by the pipe character: |

    Only the first value (Name) is required, but if you do not specify the third value (DefaultValue), the field list is not populated at design time. You can provide only the Name value and no pipes, or if you wish to provide some, but not all of the values, simply provide pipes with no space between them for the missing values. For example, <%ProductID||||False|%>

    When you add SQL parameters to a report, ActiveReports displays an 'Enter Report Parameters' dialog where the user can enter the values to fetch from the database. The steps to add a parameter to an SQL query are as follows:

    1. In the Detail section band, click the DataSource icon to view the Report Data Source dialog.
    2. Connect the report to 'Nwind.mdb' data source, an OleDB data source. See Microsoft OLEDB Provider for more information.
    3. In the Query field, enter a SQL query like the one below, which contains the parameter syntax to prompt for parameter values at runtime.
      Query
      Copy Code
      SELECT * FROM Products
      INNER JOIN (Orders INNER JOIN [Order Details] ON Orders.OrderID= [Order Details].OrderID)
      ON Products.ProductID = [Order Details].ProductID
      WHERE Products.SupplierID = <%SupplierID|Enter Supplier ID|7|||%>
      AND OrderDate >= #<%OrderDate|Order date from|11/1/1994|D|true|yyyy/MM/dd%>#                 
      AND Discontinued = <%Discontinued|Is this checked?|true|B||%>
      
             
    4. Click OK to save the data source and return to the report design surface.

      The SQL query above causes ActiveReports to display the following dialog to the user. The user can accept these or input other values to select report data.
      SQL Parameters Dialog

    Add Parameter from Report Explorer

    The steps to add a parameter from Report Explorer are as follows:

    1. In the Report Explorer, right-click the Parameters node and select Add. This adds a parameter (Parameter1) as a child to the Parameters node.
      Add Parameter in Report Explorer
    2. Select the added parameter to open the Properties Panel and set values in the following properties, the description of each of which is given in the previous section:
      • Name
      • DefaultValue
      • Prompt
      • PromptUser
      • Type
      • DisplayFormat
    3. Pass the parameter to a field on the report.

    Add Parameters at Runtime

    You can add, edit, and delete parameters at runtime. The following code demonstrates how to add a parameter and display its value in a Textbox control.

    1. Double-click in the gray area below the report to create an event-handling method for the ReportStart event.
    2. Add code to the handler to set parameters at runtime.

      To write the code in Visual Basic.NET

      Visual Basic.NET code. Paste at beginning of code view. 
      Copy Code

      Imports GrapeCity.ActiveReports.SectionReportModel

      Visual Basic.NET code. Paste INSIDE the ReportStart event.
      Copy Code

      Dim myParam1 As New Parameter()
      myParam1.Key = "myParam1"
      myParam1.Type = Parameter.DataType.String

      'Set to False if you do not want input from user.
      myParam1.PromptUser = True
      myParam1.Prompt = "Enter Data:"
      myParam1.DefaultValue = "Default Value"
      Me.Parameters.Add(myParam1);

      'Set to True to display parameter dialog box when report is run.
      Me.ShowParameterUI = True

      To write the code in C#

      C# code.  Paste at beginning of code view.
      Copy Code

      using GrapeCity.ActiveReports.SectionReportModel;

      C# code. Paste INSIDE the ReportStart event.
      Copy Code

      Parameter myParam1 = new Parameter();
      myParam1.Key = "myParam1";
      myParam1.Type = Parameter.DataType.String;

      //Set to false if you do not want input from user.
      myParam1.PromptUser = true;
      myParam1.Prompt = "Enter Data:";
      myParam1.DefaultValue = "Default Value";
      this.Parameters.Add(myParam1);

      //Set to true to display parameter dialog box when report is run.
      this.ShowParameterUI = true;

    3. In the design view, click the gray area below the report to select it and open the Properties Panel.
    4. Click the events icon in the Properties Panel to display available events for the report.
    5. Double-click FetchData. This creates an event-handling method for the report's FetchData event.
    6. Add code to the handler to pass the parameter at runtime.

      To write the code in Visual Basic.NET

      Visual Basic.NET code. Paste INSIDE the FetchData event.
      Copy Code
      'Set textbox text equal to the value of the parameter.
      Me.txtParam1.Text = Me.Parameters("myParam1").Value

      To write the code in C#

      C# code. Paste INSIDE the FetchData event.
      Copy Code
      //Set textbox text equal to the value of the parameter.
      this.txtParam1.Text = this.Parameters["myParam1"].Value;
      The run-time implementation above displays the following dialog. You can enter any text in this prompt dialog and display it on the report.
      Enter Report Parameters at Run Time

    Prompt for Parameter Values

    In order to prompt the user for parameter values, all of the following must be in place:

    When there are parameters in the collection and the ShowParameterUI property is set to True, the user prompt automatically displays when the report is run. When the user enters the requested values and clicks the OK button, the report gets displayed using the specified values.

    Values of a parameter added through the Report Explorer can be applied to a parameter in the SQL query by specifying the param: prefix for a parameter in the SQL query. This prefix relates the current parameter to the one in the Report Explorer.

    For e.g., SELECT * FROM CUSTOMERS WHERE CustomerName = '<%param:Parameter1%>'. In this case, the parameter with the param: prefix in the SQL query is updated with values of the corresponding parameter in the Report Explorer.

    Note: Within the same report, you can prompt users for some parameters and not for others by setting the PromptUser property to True for some and False for others. However, if the report object's ShowParameterUI property is set to False, the user prompt does not display any parameters, regardless of its PromptUser setting.