When you followed the instructions in the section Project Setup to prepare for this tutorial, you placed a CrystalReportViewer control on the Web or Windows Form. In the previous steps, you added a CustomersByCity report to the project.
In this section, you instantiate the CustomersByCity report and bind it to the CrystalReportViewer control. Then you test whether the report displays correctly when current values have not been set for its parameter field.
You can instantiate and bind the report two 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 the CustomersByCity report as an embedded report and bind it to the CrystalReportViewer control
Private customersByCityReport As CustomersByCity
private CustomersByCity customersByCityReport;
ConfigureCrystalReports() method, instantiate the report wrapper class.
Note You created the ConfigureCrystalReports() method in Project Setup.
customersByCityReport = New CustomersByCity()
customersByCityReport = new CustomersByCity();
myCrystalReportViewer.ReportSource = customersByCityReport
crystalReportViewer.ReportSource = customersByCityReport;
Note The CrystalReportViewer control instance is accessible in the code because you added the control to your Web or Windows form. If Intellisense does not recognize the CrystalReportViewer control instance, verify that the CrystalReportViewer control has been added as a class-level declaration to this code-behind class.
You are now ready to build and run your project. It is expected that the report loading will fail, because code has not yet been written to set a value for the City parameter field.
To instantiate the CustomersByCity report as a non-embedded report and bind it to the CrystalReportViewer control
Private customersByCityReport As ReportDocument
private ReportDocument customersByCityReport;
Note The ReportDocument class is a member of the CrystalDecisions.CrystalReports.Engine namespace. You have 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.customersByCityReport = New ReportDocument()
customersByCityReport = 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("CustomersByCity.rpt")
string reportPath = Server.MapPath("CustomersByCity.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 & "\" & "CustomersByCity.rpt"
string reportPath = Application.StartupPath + "\\" + "CustomersByCity.rpt";
Load() method of the ReportDocument instance and pass into it the reportPath string variable.customersByCityReport.Load(reportPath)
customersByCityReport.Load(reportPath);
myCrystalReportViewer.ReportSource = customersByCityReport
crystalReportViewer.ReportSource = customersByCityReport;
Whether you have chosen to instantiate an embedded report class or a non-embedded report class (ReportDocument), the variable name used is the same: customersByCityReport. This allows you to use a common set of code in the procedures that follow.
You are now ready to build and run your project. It is expected that the report loading will fail, because code has not yet been written to set a value for the City parameter field. You'll add a value for the City parameter field later in this tutorial.
To test the loading of the CustomersByCity report
\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.
The CustomersByCity report does not display. It displays after you add a value for the City parameter field later in this tutorial.
Note Results may vary, depending on the version of Crystal Reports that you use. In more recent versions, you can see a form requesting that you provide parameter values for that report. In earlier versions, a "Missing parameter field current value" exception is thrown. In either case, you must add further code to create a fully functional application.
Continue to Setting Parameters Manually In Code.
Tutorials: Reading and Setting Discrete Parameters | Tutorials and Sample Code | Tutorials' Sample Code Directory