Telerik Reporting

The ReportViewer control uses the ASP.NET session state to preserve the report instance assigned to its Report property during page postbacks and for some other design reasons as well. Sometimes because of different requirements the web application has to be configured to use an out-proc session state such as SQLServer or StateServer. When this is the case, the report instance has to be serialized and deserialized in contrast to the InProc session state mode where the report just stays in memory and is available at any time as it is. However, during the deserialization of the session object, the report is instantiated again through its default constructor which leads to the following limitations:

  • The report should have a default constructor (constructor without parameters i.e. parameterless constructor).
  • All modifications made to the report instance, after the default constructor has been called are ignored.

Additionally the values of the report’s DataSource and DataMemeber properties and the current values of the report parameters are also preserved in ASP.NET session state. The latter is especially important and can be used to pass values from the page with the ReportViewer control to the report definition. This is achieved by using the parameters in expressions where they are supported (e.g. Value properties; Filter, Sorting and Grouping expressions; etc.).

As the value of the DataSource property is kept in the ASP.NET session state, it is also recommended to use the NeedDataSource event to populate the report with data that when in an out-proc session mode. Otherwise the Session will either become very large, especially when using large datasets or the data for the report will not be serialized at all if it cannot be binary serialized/deserialized (e.g. business objects, anonymous types, data adapters, etc.). Again you can use the parameters' values to pass specific information needed to retrieve the relevant data in the NeedDataSource event.

Example 1: Modifying the Value of a TextBox item using an expression and a report parameter.

The goal is to change the value of a TextBox item in the report, according to the selected value in a drop down list:

  1. The SelectedIndexChanged event handler of the DropDownList:
    CopyC#
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Report report = (Report)this.ReportViewer1.Report;
        report.ReportParameters["Year"].Value = ((DropDownList)sender).SelectedItem.Value;
    }
    CopyVB.NET
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        Dim report As Telerik.Reporting.Report = CType(Me.ReportViewer1.Report, Telerik.Reporting.Report)
        report.ReportParameters("Year").Value = CType(sender, DropDownList).SelectedItem.ToString()
    End Sub
  2. The expression for the Value property of the TextBox item:
  3. Setting the report parameter:

Example 2: Using report parameters to retrieve specific data.

The goal is to retrieve a name depending on the selected year from a drop down list:

  1. The SelectedIndexChanged event handler implementation of the DropDownList:
    CopyC#
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Report report = (Report)this.ReportViewer1.Report;
        report.ReportParameters["Year"].Value = ((DropDownList)sender).SelectedItem.Value;
    }
    CopyVB.NET
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        Dim report As Telerik.Reporting.Report = CType(Me.ReportViewer1.Report, Telerik.Reporting.Report)
        report.ReportParameters("Year").Value = CType(sender, DropDownList).SelectedItem.Value
    End Sub
  2. Expressions for the TextBox items:

  3. Setting the report parameter:

  4. The NeedDataSource event of the report:

    CopyC#
    private void Report2_NeedDataSource(object sender, EventArgs e)
    {
        Telerik.Reporting.Processing.Report report = (Telerik.Reporting.Processing.Report)sender;
        report.DataSource = GetData((string)report.Parameters["Year"]);
    }
    CopyVB.NET
    Private Sub Report2_NeedDataSource(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.NeedDataSource
        Dim report As Telerik.Reporting.Processing.Report = CType(sender, dTelerik.Reporting.Processing.Report)
        report.DataSource = GetData(report.Parameters("Year"))
    End Sub
  5. A sample method with a parameter to retrieve the relevant data. You should implemented your way for getting the data here:

    CopyC#
    static DataTable GetData(string year)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Name", typeof(string));
        if ("2007" == year)
        {
            table.Rows.Add("Peter");
        }
        else if ("2008" == year)
        {
            table.Rows.Add("Scott");
        }
        return table;
    }
    CopyVB.NET
    Private Shared Function GetData(ByVal year) As DataTable
        Dim table As DataTable
        table = New DataTable()
        table.Columns.Add("Name", GetType(String))
        If (year = "2007") Then
            table.Rows.Add("Peter")
        ElseIf (year = "2008") Then
            table.Rows.Add("Scott")
        End If
        Return table
    End Function

See Also