Share via


Binding to a Non-embedded Report Loaded into the ReportDocument Class

Object Model

This report binding scenario uses ReportDocument (see Report Binding with ReportDocument Object Model).

Location of Reports

Reports are located in the file directory.

Description

This report binding scenario allows you to use the more powerful ReportDocument object model with non-embedded reports, (reports that are external to your project).

In Binding to a File Directory Path in Code, you bind to reports by their file directory path strings; however, rather than bind the directory paths directly to the viewer, you pass them into the ReportDocument.Load() method. This allows you to use the ReportDocument object model.

Note

For more information see Which Object Model Should I Use?.

Also, because each external report is loaded into the common class ReportDocument, you can develop a report selection process that shares common report binding code for all reports. This is similar to the code in the Implementation section of the following report binding scenario Binding to an Embedded Report Class Upcast to ReportDocument).

Pros

  • Low maintenance: reports can be added, removed or modified without recompiling your application.
  • Extensive programmatic interaction: provides access to the powerful ReportDocument object model.
  • Less coding through code sharing: similar to use of report binding scenario Binding to an Embedded Report Class Upcast to ReportDocument), reports can share code, because all reports are loaded into the common ReportDocument class.

Cons

  • Limited distribution: reports must be on the same machine as the application. (Access from the Web server's ASPNET user account to other servers on the network is typically restricted.)
  • Added deployment effort: reports must be distributed along with the application at the correct relative path.
  • Less secure report source: there is a risk of reports being relocated/removed on the deployment machine at runtime.

To bind to a non-embedded report using the ReportDocument object model

Note

This procedure works only with a project that has been created from Project Setup. Project Setup contains specific namespace references and code configuration that is required for this procedure, and you will be unable to complete the procedure without that configuration. Therefore, before you begin this procedure, you must first follow the steps in Project Setup.

  1. Add an "Imports" [Visual Basic] or "using" [C#] statement to the top of the class for the CrystalDecisions.CrystalReports.Engine namespace.
> [!NOTE]
> <P>Declaring this namespace is required in order to access the ReportDocument class without a namespace prefix.</P>


``` vb
Imports CrystalDecisions.CrystalReports.Engine
```

``` csharp
using CrystalDecisions.CrystalReports.Engine;
```
  1. Locate the World Sales Report.rpt file in the General Business subdirectory for your particular version of Crystal Reports. For information about sample reports, see Sample Reports' Directory.

  2. Copy the complete file directory path to the clipboard, including World Sales Report.rpt.

  3. Within the ConfigureCrystalReports() method (that you have created in Project Setup), declare a reportPath string variable and assign a string that contains the World Sales Report file directory path that you copied in the previous step.

    Dim reportPath As String = _
       "C:\Program Files\Microsoft Visual Studio 9.0\" _
       & "Crystal Reports\Samples\En\Reports\General Business\" _
       & "World Sales Report.rpt"
    
    string reportPath = 
       "C:\\Program Files\\Microsoft Visual Studio 9.0\\"
       + "Crystal Reports\\Samples\\En\\Reports\\General Business\\"
       + "World Sales Report.rpt";
    
  4. Below the string declaration, declare an instance of ReportDocument.

    Dim myReportDocument As ReportDocument = New ReportDocument()
    
    ReportDocument reportDocument = new ReportDocument();
    
  5. Load the string variable. which contains the file directory path, to the non-embedded report into ReportDocument.

    myReportDocument.Load(reportPath)
    
    reportDocument.Load(reportPath);
    
  6. Assign the ReportDocument instance (which now contains the loaded non-embedded report) to the ReportSource property of the CrystalReportViewer control.

    myCrystalReportViewer.ReportSource = myReportDocument
    
    crystalReportViewer.ReportSource = reportDocument;
    
  7. To view the report, build and run your project.

See Also