Binding the Report
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
- Open the default Code-Behind class, Default.aspx.cs or Default.aspx.vb.
- 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]
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.
- Add a new class-level ArrayList, call it stockValues.[Visual Basic]
Private stockValues As ArrayList[C#]
private ArrayList stockValues;
- 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[C#]
private ReportDocument StockObjectsReport;
- 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")[C#]
string reportPath = Server.MapPath("StockObjects.rpt");
- Add two line breaks, and instantiate the ReportDocument class.[Visual Basic]
stockObjectsReport = New ReportDocument()[C#]
stockObjectsReport = new ReportDocument();
- 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.
- Next, set the data source of the report to the stockValues ArrayList.[Visual Basic]
- Finally, bind the ReportSource property of the CrystalReportViewer to the ReportDocument instance.[Visual Basic]
myCrystalReportViewer.ReportSource = stockObjectsReport[C#]
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.