Share via


Binding the Report and Setting the DataSource to the Populated DataSet

In this section, you learn how to instantiate the report that you have created, populate the report's DataSet, and display the report in the CrystalReportViewer control. You populate the report through the assignment of its SetDataSource property to the populated DataSet, which is returned from the CustomerDataSet property of the DataSetConfiguration class. Finally, you bind the populated report to the CrystalReportViewer control.

You can instantiate and bind the report in the following ways:

  • As an embedded report.
  • As a non-embedded report.

Choose from one (but not both) of the step procedures below.

To instantiate and bind a non-embedded report 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 customerReport. Set its access modifier to private.

> [!NOTE]
> <P>The ReportDocument class is a member of the CrystalDecisions.CrystalReports.Engine namespace. You added an "Imports"[Visual Basic] or "using"[C#] declaration for this namespace in <A href="ms227453(v=vs.90).md">Project Setup</A>. When you instantiate ReportDocument and load a report into the namespace, you gain access to the report through the SDK, without embedding the report.</P>


``` vb
Private customerReport As ReportDocument
```

``` csharp
private ReportDocument customerReport;
```
  1. Within the ConfigureCrystalReports() method (which you added during one of the procedures in Project Setup), instantiate the ReportDocument class.

    customerReport = New ReportDocument()
    
    customerReport = new ReportDocument();
    
  2. 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("Customer.rpt")
      
      string reportPath = Server.MapPath("Customer.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 & "\" &
      "Customer.rpt"
      
      string reportPath = Application.StartupPath + "\\" +
      "Customer.rpt";
      
  3. Call the Load() method of the ReportDocument instance and pass into it the reportPath string variable.

``` vb
customerReport.Load(reportPath)
```

``` csharp
customerReport.Load(reportPath);
```
  1. Declare a DataSet and assign to it the DataSetConfiguration.CustomerDataSet property.

    Dim myDataSet As DataSet = DataSetConfiguration.CustomerDataSet
    
    DataSet dataSet = DataSetConfiguration.CustomerDataSet;
    
  2. Call the SetDataSource() method of the customerReport ReportDocument instance and pass into it the DataSet instance.

``` vb
customerReport.SetDataSource(myDataSet)
```

``` csharp
customerReport.SetDataSource(dataSet);
```
  1. On the next line, beneath the report loading, bind the ReportSource property of the CrystalReportViewer to the ReportDocument instance.

    myCrystalReportViewer.ReportSource = customerReport
    
    crystalReportViewer.ReportSource = customerReport;
    

To instantiate and bind an embedded report to the CrystalReportViewer control

  1. Open the Web or Windows Form.

  2. From the View menu, click Code.

  3. Above the class signature, add an "Imports"[Visual Basic] or "using"[C#] declaration to the top of the class for the System.Data namespace if it is not there already.

``` vb
Imports System.Data
```

``` csharp
using System.Data;
```
  1. Add a new class-level declaration for the Customer report wrapper class, with the variable name customerReport. Set its access modifier to private.

    Private customerReport As Customer
    
    private Customer customerReport;
    
  2. Within the ConfigureCrystalReports() method, instantiate the report wrapper class.

    Note

    You created the ConfigureCrystalReports() method in Project Setup.

    customerReport = New Customer()
    
    customerReport = new Customer();
    
  3. On the next line beneath the report instantiation, declare a DataSet.

This step and the next step separate the declaration of the variable from the assignment of the variable. Each line of code is kept separate because, in a Web Site addendum to this tutorial, you will refactor the variable assignment into a code block that caches the DataSet in the ASP.NET Cache object.

``` vb
Dim myDataSet As DataSet
```

``` csharp
DataSet dataSet;
```
  1. Assign the DataSet instance to the DataSetConfiguration.CustomerDataSet property.

    myDataSet = DataSetConfiguration.CustomerDataSet
    
    dataSet = DataSetConfiguration.CustomerDataSet;
    
  2. Call the SetDataSource() method of the CustomerReport instance and pass into it the DataSet instance.

``` vb
customerReport.SetDataSource(myDataSet)
```

``` csharp
customerReport.SetDataSource(dataSet);
```
  1. Bind the ReportSource property of the CrystalReportViewer control to the CustomerReport instance.

    myCrystalReportViewer.ReportSource = customerReport
    
    crystalReportViewer.ReportSource = customerReport;
    

To test the loading of the Customer report and its populated DataSet

You are now ready to build and run your project.

  1. From the Build menu, click 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.

  5. The Customer report displays and shows the populated data that you placed in the DataSet.

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

If you are building a Windows project, you have now completed the tutorial.

If you are building a Web Site, continue to Caching the DataSet in a Web Site.