Sei sulla pagina 1di 4

Binding the Report

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.

You can instantiate and bind the report in two ways:

• 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

1. Open the Web or Windows Form.

2. From the View menu, click Code.

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]

Private northwindCustomersReport As NorthwindCustomers


[C#]
private NorthwindCustomers northwindCustomersReport;

4. Within the Con f igu reCrys ta lRepor t smethod,


() instantiate the report wrapper class.

Note You created the Con f igu reCrys ta lRepor t smethod


() in Project Setup.

[Visual Basic]

northwindCustomersReport = New NorthwindCustomers()


[C#]
northwindCustomersReport = new NorthwindCustomers();

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

1. Open the Web or Windows Form.

2. From the View menu, click Code.

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]

Private northwindCustomersReport As ReportDocument


[C#]
private ReportDocument northwindCustomersReport;

Note The ReportDocument class is a member of the CrystalDecisions.CrystalReports.Engine


namespace. You have added an " Impor t s " [V i sua l Bas
ori c"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.

4. Within the ConfigureCrystalReports() method (that you have created in Project Setup),

instantiate the ReportDocument class.


[Visual Basic]

northwindCustomersReport = New ReportDocument()


[C#]
northwindCustomersReport = new ReportDocument();

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]

Dim reportPath As String = Server.MapPath("NorthwindCustomers.rpt")

[C#]

string reportPath = Server.MapPath("NorthwindCustomers.rpt");

• For a Windows project, concatenate the 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.
[Visual Basic]

Dim reportPath As String = Application.StartupPath & "\" & "NorthwindCustomers.rpt"

[C#]

string reportPath = Application.StartupPath + "\\" + "NorthwindCustomers.rpt";

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);

7. Bind the ReportSource property of the CrystalReportViewer to the ReportDocument instance.


[Visual Basic]

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.

To test the loading of the NorthwindCustomers report

1. From the Build menu, select Build Solution.

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

executable in the \ b in \ [V i sua l Bas


ori c
\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.

4. From the Debug menu, click Start.

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.

Continue to Adding the Report Logon Code.

See Also

Tutorials: Logging onto a Secure SQL Server Database Using SQL Authentication | Tutorials and
Sample Code | Tutorials' Sample Code Directory

Potrebbero piacerti anche