Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
In Project Setup, you placed a CrystalReportViewer control on the Web or Windows Form. In the
previous step, you have added a NorthwindCustomers report to the project.
In this section, you instantiate the NorthwindCustomers 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.
• As an embedded report.
• As a non-embedded report.
Note Visual Studio 2005 supports only non-embedded reports for Web Sites.
Choose from one (but not both) of the step procedures below.
• If you use embedded reports, follow the next step procedure to instantiate the report as an
embedded report.
• If you use non-embedded reports, follow the second step procedure to instantiate the report
as a non-embedded report.
To instantiate the NorthwindCustomers report as an embedded report and bind it to the CrystalReportViewer control
3. Add a new class-level declaration for the NorthwindCustomers report wrapper class, using
the variable name northwindCustomersReport. Set its access modifier to private.
[Visual Basic]
[Visual Basic]
5. On the next line beneath the report instantiation, bind the ReportSource property of the
CrystalReportViewer control to the instantiated report class (variable name:
northwindCustomersReport).
[Visual Basic]
myCrystalReportViewer.ReportSource = northwindCustomersReport
[C#]
crystalReportViewer.ReportSource = northwindCustomersReport;
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 log onto the database.
To instantiate the NorthwindCustomers report as a non-embedded report and bind it to the CrystalReportViewer control
3. Add a new class-level declaration for the ReportDocument report wrapper class, using the
variable name northwindCustomersReport. Set its access modifier to private.
[Visual Basic]
4. Within the ConfigureCrystalReports() method (that you have created in Project Setup),
5. Declare a string variable, name it reportPath, and assign to it a runtime path to the local
report. This path is determined differently for Web Sites and Windows projects:
• For a Web Site, pass the name of the local report file as a string parameter into the
Server.MapPath() method. This maps the local report to the hard drive file directory path
at runtime.
[Visual Basic]
[C#]
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.
[Visual Basic]
[C#]
6. Call the Load() method of the ReportDocument instance and pass into it the reportPath
string variable.
[Visual Basic]
northwindCustomersReport.Load(reportPath)
[C#]
northwindCustomersReport.Load(reportPath);
myCrystalReportViewer.ReportSource = northwindCustomersReport
[C#]
crystalReportViewer.ReportSource = northwindCustomersReport;
Whether you have chosen to instantiate an embedded report class or a non-embedded report class
(ReportDocument), the variable name used is the same: northwindCustomersReport. 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 log onto the database.
2. If you have any build errors, go ahead and fix them now.
3. If you use a non-embedded report in a Windows project, locate the compiled Windows
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 NorthwindCustomers report does not display, because the database logon code has not
been added.
Note Results may vary, depending on the version of Crystal Reports that you use. For
example, if you have Crystal Reports 10 or higher installed, a form appears and requests that
you provide database logon information for that report. This is a new feature of Crystal
Reports Developer. If you are running a previous version of Crystal Reports, an exception is
thrown. In either case, you must follow the next step procedure to create a fully functional
application.
5. Return to Visual Studio and click Stop to exit from debug mode.
See Also
Tutorials: Logging onto a Secure SQL Server Database Using SQL Authentication | Tutorials and
Sample Code | Tutorials' Sample Code Directory