Crystal Reports XI Release 2  

Binding the Report

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:

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

  1. Open the Web or Windows Form.
  2. From the View menu, click Code to view the code-behind class for this Web or Windows Form.
  3. Add a new class-level declaration for the CustomersByCity report wrapper class, with the variable name customersByCityReport. Set its access modifier to private.
    [Visual Basic]
    Private customersByCityReport As CustomersByCity
    [C#]
    private CustomersByCity customersByCityReport;
  4. Within the ConfigureCrystalReports() method, instantiate the report wrapper class.
    Note   You created the ConfigureCrystalReports() method in Project Setup.
    [Visual Basic]
    customersByCityReport = New CustomersByCity()
    [C#]
    customersByCityReport = new CustomersByCity();
  5. On the next line beneath the report instantiation, bind the ReportSource property of the CrystalReportViewer control to the instantiated report class (variable name: customersByCityReport).
    [Visual Basic]
    myCrystalReportViewer.ReportSource = customersByCityReport
    [C#]
    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

  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, with the variable name customersByCityReport. Set its access modifier to private.
    [Visual Basic]
    Private customersByCityReport As ReportDocument
    [C#]
    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.
  4. Within the ConfigureCrystalReports() method (which you added during one of the procedures in Project Setup), instantiate the ReportDocument class.
    [Visual Basic]
    customersByCityReport = New ReportDocument()
    [C#]
    customersByCityReport = 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("CustomersByCity.rpt")
      [C#]
      string reportPath = Server.MapPath("CustomersByCity.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 & "\" & "CustomersByCity.rpt"
      [C#]
      string reportPath = Application.StartupPath + "\\" + "CustomersByCity.rpt";
  6. Call the Load() method of the ReportDocument instance and pass into it the reportPath string variable.
    [Visual Basic]
    customersByCityReport.Load(reportPath)
    [C#]
    customersByCityReport.Load(reportPath);
  7. On the next line, beneath the report loading, bind the ReportSource property of the CrystalReportViewer to the ReportDocument instance.
    [Visual Basic]
    myCrystalReportViewer.ReportSource = customersByCityReport
    [C#]
    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

  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 \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.
  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 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.
  5. Return to Visual Studio and click Stop to exit from debug mode.

Continue to Setting Parameters Manually In Code.

See Also

Tutorials: Reading and Setting Discrete Parameters | Tutorials and Sample Code | Tutorials' Sample Code Directory