Binding the Report

Visual Studio 2005

In Project Setup, you placed a CrystalReportViewer control on the Web or Windows Form. In the previous procedure, you added a StockObjects report to the project.

In this section, you will bind the Stock Objects report to the Crystal Report Viewer, set the data source of the report to an Object Collection, and populate the Object Collection programmatically.

To instantiate the StockObjects report as a non-embedded report and bind it to the CrystalReportViewer control

  1. Open the default Code-Behind class, Default.aspx.cs or Default.aspx.vb.
  2. Above the class signature, add an "Imports" [Visual Basic] or "using" [C#] declaration to the top of the class for the System.Collections namespace.
    [Visual Basic]
    Imports System.Collections
    using System.Collections;
    Note   This reference gives you access to the ArrayList class. ArrayList implements ICollection. This qualifies ArrayList as one of several class types that can be used to build an object collection that is recognized by Crystal Reports.
  3. Add a new class-level ArrayList, call it stockValues.
    [Visual Basic]
    Private stockValues As ArrayList
    private ArrayList stockValues;
  4. Add a new class-level declaration for the ReportDocument report wrapper class, with the variable name stockObjectsReport. Set its access modifier to private.
    [Visual Basic]
    Private stockObjectsReport As ReportDocument
    private ReportDocument StockObjectsReport;
  5. Within the ConfigureCrystalReports() method you created in Project Setup, declare a string variable, name it reportPath, and assign to it a runtime path to the local report.

    Pass the name of the local report file as a string parameter into the Server.MapPath() method. This maps the local report to the file path at runtime.

    [Visual Basic]
    Dim reportPath As String = Server.MapPath("StockObjects.rpt")
    string reportPath = Server.MapPath("StockObjects.rpt");
  6. Add two line breaks, and instantiate the ReportDocument class.
    [Visual Basic]
    stockObjectsReport = New ReportDocument()
    stockObjectsReport = new ReportDocument();
  7. On the next line, Call the Load() method of the ReportDocument instance and pass into it the reportPath string variable.
    [Visual Basic]
    Note   The ReportDocument class is a member of the CrystalDecisions.CrystalReports.Engine namespace. You added an "Imports" [Visual Basic] or "using" [C#] declaration for this namespace in Project Setup. When you instantiate ReportDocument and load a report, you gain access to the report through the SDK.
  8. Next, set the data source of the report to the stockValues ArrayList.
    [Visual Basic]
  9. Finally, bind the ReportSource property of the CrystalReportViewer to the ReportDocument instance.
    [Visual Basic]
    myCrystalReportViewer.ReportSource = stockObjectsReport
    crystalReportViewer.ReportSource = stockObjectsReport;

At this point, the Stock Objects report is bound to the Crystal Report Viewer and the page displays the correct report; however, the report is currently bound to an empty data source, thus the report has no information to display. In the next section, you will programmatically populate the stockValues ArrayList with sample data.

Continue to Populating the Object Collection Programmatically.

See Also

Connecting to Object Collections | Tutorials and Sample Code | Tutorials' Sample Code Directory