ActiveReports 18 .NET Edition
Report Authors / 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 SQL Query

    Section report allows you to add parameters to a report's Parameters collection via the SQL query. You can add build queries with parameters using following easy steps:

    1. Create a new section report or open an existing report and 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 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||%>
      


      Report Data Source Dialog    

      You can also add SQL query using the Visual Query Designer. To do so, click on the Query Designer button, select the Design option, and select the desired fields to create the query. To preview the query, click on the SQL option and then click Save.

      Visual Query Designer

      Note: In section reports, the Visual Query Designer is only available for the OLEDB, SQL, and Custom Data Sources.
    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 at runtime. The user can accept these or input other values to select report data.

      SQL Parameters Dialog

    The values below are used while creating the parameter syntax. The values in the parameter syntax are separated by the pipe character (|).

    For creating a parameter syntax, the first value (Name) is required. However, if the DefaultValue is not specified, the fields are not populated at the design time. In the syntax, you can provide only the Name value (without pipe character), or you can provide some values along with the Name value and use pipes without space for the missing values. For example, <%ProductID||||False|%>.

    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 them 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.