Share via


Binding the Report

When you followed the instructions in Project Setup to prepare for this tutorial, you have 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 bind the file directory path of the CustomersByCity report to the CrystalReportViewer control. Then you test whether the report displays correctly when current values have not been set for its parameter field.

To bind the file directory path of the CustomersByCity report 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. Locate the ConfigureCrystalReports() method (that you have created in Project Setup).

  4. 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.

      Dim reportPath As String = Server.MapPath("CustomersByCity.rpt")
      
      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.

      Dim reportPath As String = Application.StartupPath & "\" & "CustomersByCity.rpt"
      
      string reportPath = Application.StartupPath + "\\" + "CustomersByCity.rpt";
      
  5. Assign the file directory path of the CustomersByCity report to the ReportSource property of the CrystalReportViewer control.

    myCrystalReportViewer.ReportSource = reportPath
    
    crystalReportViewer.ReportSource = reportPath;
    

To test the loading of the CustomersByCity report

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.

  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]
> <P>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.</P>
  1. From the Debug menu, click Start.

    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.

  2. Return to Visual Studio and click Stop to exit from debug mode.