Share via


Binding the Report Without Logon Code

In Project Setup, you have 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 bind the file directory path of the NorthwindCustomers report to the CrystalReportViewer control. Then you test whether the report displays correctly when the database logon code has not been set.

To bind the file directory path of the NorthwindCustomers report to the CrystalReportViewer control

  1. Open the Web or Windows Form.

  2. From the View menu, click Code.

  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("NorthwindCustomers.rpt")
      
      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.

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

    myCrystalReportViewer.ReportSource = reportPath
    
    crystalReportViewer.ReportSource = reportPath;
    

To test the loading of the NorthwindCustomers 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 log onto the database.

  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\debug\ 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.

The NorthwindCustomers report will not display. It will display after you add the database logon code.


> [!NOTE]
> <P>Results may vary, depending on the version of Crystal Reports that you use. For example, if you have Crystal Reports 10 and up installed, you are prompted for database logon information for that report. 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.</P>
  1. Return to Visual Studio and click Stop to exit from debug mode.