New to Telerik Reporting? Download free 30-day trial

Using Report Parameters Programmatically

In the report viewing application you can populate the values of ReportParameters collection members prior to displaying the report.

Report1 report = new Report1();
report.ReportParameters["ManagerID"].Value = "123";
Dim report As New Report1()
report.ReportParameters("ManagerID").Value = "123"

At runtime you can access the report parameters through the Telerik.Reporting.Processing.Report.Parameters dictionary. Each Parameter object contains resolved available values, current value and label. The label returns the currently selected DisplayMember from the available values (if available values are defined).

Prior to version 2010 Q1 the processing report exposes a dictionary containing only the current values of the report parameters.

Example:

  1. Create a Telerik Report
  2. Use the instructions from the How to Connect to a SQL Database article to bind to the uspGetManagerEmployees stored procedure from the AdventureWorks table.
  3. Leave the Value for the @ManagerID data source parameter empty and finish the wizard.
  4. Create the following layout for the report

    The layout of the report definition for this example

  5. Add a report parameter and name it ManagerID.

  6. Change its Type to Integer.
  7. Set its Visible property to True.
  8. Expand the AvailableValues and bind it to SqlDataSource component with the following query:

    SELECT        M.ManagerID, C.FirstName + ' ' + C.LastName AS Name
    FROM          (SELECT DISTINCT ManagerID
                   FROM      HumanResources.Employee) AS M INNER JOIN
                             HumanResources.Employee AS E ON M.ManagerID = E.EmployeeID INNER JOIN
                             Person.Contact AS C ON E.ContactID = C.ContactID
    
  9. Set the ValueMember to = Fields.ManagerID.
  10. Set the DisplayMember to = Fields.Name.
  11. Add grouping for the report with = Fields.ManagerID as grouping expression.
  12. Wire the NeedDataSource event of the report.
  13. In the report.cs file set the DataSource property to null ( Nothing in VB.NET) so that NeedDataSource event is fired.
  14. Add the following code to the NeedDataSource event handler:

    private void Report1_NeedDataSource(object sender, System.EventArgs e)
    {
        //Take the Telerik.Reporting.Processing.Report instance
        Telerik.Reporting.Processing.Report report = (Telerik.Reporting.Processing.Report)sender;
    
        // Transfer the value of the processing instance of ReportParameter
        // to the parameter value of the sqlDataSource component
        this.sqlDataSource1.Parameters[0].Value = report.Parameters["ManagerID"].Value;
    
        // Set the SqlDataSource component as it's DataSource
        report.DataSource = this.sqlDataSource1;
    }
    
    Private Sub Report1_NeedDataSource(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.NeedDataSource
        'Take the Telerik.Reporting.Processing.Report instance
        Dim report As Telerik.Reporting.Processing.Report = DirectCast(sender, Telerik.Reporting.Processing.Report)
    
        ' Transfer the value of the processing instance of ReportParameter
        ' to the parameter value of the sqlDataSource component
        Me.sqlDataSource1.Parameters(0).Value = report.Parameters("ManagerID").Value
    
        ' Set the SqlDataSource component as it's DataSource
        report.DataSource = Me.sqlDataSource1
    End Sub
    
  15. Display the Report in a report viewer.
  16. Select a parameter from the available values in the parameter editor and click Preview.

    Preview of the example report showing the properly grouped data for a particular manager Id

In this article