In this section, you learn how to instantiate the report that you have created, populate the report's DataSet, and display the report in the CrystalReportViewer control. You populate the report through the assignment of its SetDataSource property to the populated DataSet, which is returned from the CustomerDataSet property of the DataSetConfiguration class. Finally, you bind the populated report to the CrystalReportViewer control.
You can instantiate and bind the report in the following ways:
Note Visual Studio 2005 supports only non-embedded reports for Web Sites.
Choose from one (but not both) of the step procedures below.
To instantiate and bind an embedded report to the CrystalReportViewer control
"Imports"[Visual Basic] or "using"[C#] declaration to the top of the class for the System.Data namespace if it is not there already.Imports System.Data
using System.Data;
Private customerReport As Customer
private Customer customerReport;
ConfigureCrystalReports() method, instantiate the report wrapper class.
Note You created the ConfigureCrystalReports() method in Project Setup.
customerReport = New Customer()
customerReport = new Customer();
Dim myDataSet As DataSet
DataSet dataSet;
This step and the next step separate the declaration of the variable from the assignment of the variable. Each line of code is kept separate because, in a Web Site addendum to this tutorial, you will refactor the variable assignment into a code block that caches the DataSet in the ASP.NET Cache object.
myDataSet = DataSetConfiguration.CustomerDataSet
dataSet = DataSetConfiguration.CustomerDataSet;
SetDataSource() method of the CustomerReport instance and pass into it the DataSet instance.customerReport.SetDataSource(myDataSet)
customerReport.SetDataSource(dataSet);
myCrystalReportViewer.ReportSource = customerReport
crystalReportViewer.ReportSource = customerReport;
You are now ready to build and run your project. Skip over the non-embedded report step procedure to the step procedure that follows it.
To instantiate and bind a non-embedded report to the CrystalReportViewer control
Private customerReport As ReportDocument
private ReportDocument customerReport;
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 into the namespace, you gain access to the report through the SDK, without embedding the report.
ConfigureCrystalReports() method (which you added during one of the procedures in Project Setup), instantiate the ReportDocument class.customerReport = New ReportDocument()
customerReport = new ReportDocument();
Server.MapPath() method. This maps the local report to the hard drive file directory path at runtime.Dim reportPath As String = Server.MapPath("Customer.rpt")
string reportPath = Server.MapPath("Customer.rpt");
Application.StartupPath property with a backslash and the local report file name. This maps the report to the same directory as the Windows executable file.
Note At compile time you will copy the report to the directory containing the executable file.
Dim reportPath As String = Application.StartupPath & "\" & "Customer.rpt"
string reportPath = Application.StartupPath + "\\" + "Customer.rpt";
Load() method of the ReportDocument instance and pass into it the reportPath string variable.customerReport.Load(reportPath)
customerReport.Load(reportPath);
Dim myDataSet As DataSet = DataSetConfiguration.CustomerDataSet
DataSet dataSet = DataSetConfiguration.CustomerDataSet;
SetDataSource() method of the customerReport ReportDocument instance and pass into it the DataSet instance.customerReport.SetDataSource(myDataSet)
customerReport.SetDataSource(dataSet);
myCrystalReportViewer.ReportSource = customerReport
crystalReportViewer.ReportSource = customerReport;
You are now ready to build and run your project.
To test the loading of the Customer report and its populated DataSet
\bin\ [Visual Basic] or \bin\debug\ [C#] subdirectory, and then copy the report to that subdirectory.
Note To have the non-embedded report loaded by the Windows executable at runtime, the report must be stored in the same directory as the Windows executable.
Note If you are developing a Web Site in Visual Studio 2005, and this is the first time you have started debugging, a dialog box appears and states that the Web.config file must be modified. Click the OK button to enable debugging.
If you are building a Windows project, continue to Conclusion.
If you are building a Web Site, continue to Caching the DataSet in a Web Site.
Tutorial: Connecting to ADO.NET DataSets | Tutorials and Sample Code | Tutorials' Sample Code Directory