ComponentOne WebReports for ASP.NET: WebReports for ASP.NET Tutorials > Rendering a Parameterized Report

Rendering a Parameterized Report

This tutorial shows how you can provide custom reports on your Web site. The sample loads a predefined report, then allows the user to change some parameters that are used to customize the report. You can provide as much customization as you want because the C1Report control allows you to change every aspect of the report. Complete the following steps:

1.   Start a new ASP.NET 2.0 project. For details creating a new application, see Creating an ASP.NET Project.

2.   Add a C1WebReport control to the form. For details on adding the component to the Toolbox, see Adding the WebReports for ASP.NET Component to a Project.

3.   Set the C1WebReport.Visible property to False since you do not want to show the report until the user has selected the parameters.

4.   Click the smart tag () located at the top right corner of the C1WebReport control to open the C1WebReport Tasks menu.

5.   In the C1WebReport Tasks menu, click Load Report. The Select a report dialog box appears where you can select a report definition file and the report name within the file.

Alternatively, the Select a report dialog box can be accessed by clicking the Load Report link at the bottom pane of the Properties window.

6.   Click the ellipsis button and locate the NWindEmbedPics.xml  file (by default, it is located in the Documents or My Documents folder at ComponentOne Samples\C1Report\C1Report\XML\SampleReports\NWindEmbedPics.xml).

7.   Select the NWindEmbedPics.xml file.

Note: The report definition file is created with a separate utility, the C1ReportDesigner. The C1ReportDesigner works like the Access report generator, and is the same designer that ships with the C1Report component.

8.   From the Report drop-down list, select the Employee Sales by Country report.

9.   Click the Load button to load the report into the control.

10.  To allow users to select the country and the year they are interested in, add two DropDownList controls and a Button control to the page. Also add two labels next to the controls, as shown in the following screen shot:

 

 

11.  Select the first drop-down list and change its (ID) to _listCountry. Then select the Items property and use the editor to enter the list of valid choices: All, UK, and US (the database does not have data for any other countries in it).

12.  Select the second drop-down list and change its (ID) to _listYear. Then select the Items property and use the editor to enter the list of valid choices: All, 1994, 1995, and 1996 (our database does not have data for any other years).

13.  Double-click the form and add the following code to the Page_Load event:

      Visual Basic

Private Sub Page_Load(sender As Object, e As System.EventArgs)

    ' Prepare SQL statement for the report

    Dim sql As String = _

        "SELECT DISTINCTROW Employees.Country, " & _

        "Employees.LastName, Employees.FirstName,

           Orders.ShippedDate, " & +

        "Orders.OrderID, [Order Subtotals].Subtotal AS

           SaleAmount " & _

        "FROM Employees INNER JOIN (Orders INNER JOIN [Order

           Subtotals] " & _

        "ON Orders.OrderID = [Order Subtotals].OrderID) ON " _

        & "Employees.EmployeeID = Orders.EmployeeID"

  

    ' Build WHERE clause

    Dim where As String = ""

    Dim country As String = _listCountry.SelectedItem.Text

    If country <> "All" Then

      where += String.Format(" Country='{0}'", country)

    End If

    Dim year As String = _listYear.SelectedItem.Text

    If year <> "All" Then

        If where.Length > 0 Then

            where += " AND"

            where += String.Format("

              Year(Orders.ShippedDate)={0}", year)

        End If

    End If

  

    ' Finish SQL statement

    If where.Length > 0 Then

        sql += " WHERE " + where

     End If

 

    ' Assign it to the report

    C1WebReport1.Report.DataSource.RecordSource = sql

End Sub

      C#

private void Page_Load[object sender, System.EventArgs e]

{

    // Prepare SQL statement for the report

    string sql = "SELECT DISTINCTROW Employees.Country, " +

        "Employees.LastName, Employees.FirstName,

          Orders.ShippedDate, " +

        "Orders.OrderID, [Order Subtotals].Subtotal AS

          SaleAmount " +

        "FROM Employees INNER JOIN (Orders INNER JOIN [Order

          Subtotals] " +

        "ON Orders.OrderID = [Order Subtotals].OrderID)ON " +

        "Employees.EmployeeID = Orders.EmployeeID";

 

    // Build WHERE clause

    string where = "";

    string country = _listCountry.SelectedItem.Text;

    if (country != "All")

        where += string.Format(" Country='{0}'", country);

    string year = _listYear.SelectedItem.Text;

    if (year != "All")

    {

        if (where.Length > 0) where += " AND";

        where += string.Format(" Year(Orders.ShippedDate)={0}", year);

    }

 

    // Finish SQL statement

    if (where.Length > 0)

        sql += " WHERE " + where;

 

    // Assign it to the report

    C1WebReport1.Report.DataSource.RecordSource = sql;

}

The code builds a SQL statement (based on the original RecordSource property in the report definition), then appends a WHERE clause that reflects the current user choices.

Finally, it assigns the RecordSource property back to the control.

14.  Double-click the Show Report button to add an event handler for the Click event. The Code Editor will open with the insertion point placed within the event handler.

15.  Insert the following code:

      Visual Basic

' Make sure the report is visible, and

' the Page_Load event handler will set it up

C1WebReport1.Visible = True

      C#

// Make sure the report is visible, and

// the Page_Load event handler will set it up

C1WebReport1.Visible = true;

This code has two purposes. First, it makes sure that the report control is visible. Second, by clicking on it the user caused a postback, which will cause your page to be reloaded and the report parameters to be applied (the code you added in the Page_Load event).

Run the project and observe the following:

In a few seconds, the browser will come back showing the initial page where you can select the report parameters. Pick a combination, and then click the Show Report button. The report will appear below the button.

Note that the C1WebReport control is smart enough to use the cache automatically even in this type of situation. It will detect any changes to the report definition and will automatically keep separate copies of the report in the cache. If you (or someone else viewing the page) selects the same parameter combination, the report will be retrieved from the cache, almost instantly.


Send comments about this topic to ComponentOne.
Copyright © 1987-2010 ComponentOne LLC. All rights reserved.