Crystal Reports XI Release 2  

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:

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

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.
    [Visual Basic]
    Imports System.Data
    [C#]
    using System.Data;
  4. Add a new class-level declaration for the Customer report wrapper class, with the variable name customerReport. Set its access modifier to private.
    [Visual Basic]
    Private customerReport As Customer
    [C#]
    private Customer customerReport;
  5. Within the ConfigureCrystalReports() method, instantiate the report wrapper class.
    Note   You created the ConfigureCrystalReports() method in Project Setup.
    [Visual Basic]
    customerReport = New Customer()
    [C#]
    customerReport = new Customer();
  6. On the next line beneath the report instantiation, declare a DataSet.
    [Visual Basic]
    Dim myDataSet As DataSet
    [C#]
    DataSet 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.

  7. Assign the DataSet instance to the DataSetConfiguration.CustomerDataSet property.
    [Visual Basic]
    myDataSet = DataSetConfiguration.CustomerDataSet
    [C#]
    dataSet = DataSetConfiguration.CustomerDataSet;
  8. Call the SetDataSource() method of the CustomerReport instance and pass into it the DataSet instance.
    [Visual Basic]
    customerReport.SetDataSource(myDataSet)
    [C#]
    customerReport.SetDataSource(dataSet);
  9. Bind the ReportSource property of the CrystalReportViewer control to the CustomerReport instance.
    [Visual Basic]
    myCrystalReportViewer.ReportSource = customerReport
    [C#]
    crystalReportViewer.ReportSource = customerReport;

You are now ready to build and run your project. Skip over the non-embedded report step procedure to the step procedure that follows it.

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.
    [Visual Basic]
    Private customerReport As ReportDocument
    [C#]
    private ReportDocument customerReport;
    Note   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 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]
    customerReport = New ReportDocument()
    [C#]
    customerReport = 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("Customer.rpt")
      [C#]
      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.
      [Visual Basic]
      Dim reportPath As String = Application.StartupPath & "\" & "Customer.rpt"
      [C#]
      string reportPath = Application.StartupPath + "\\" + "Customer.rpt";
  6. Call the Load() method of the ReportDocument instance and pass into it the reportPath string variable.
    [Visual Basic]
    customerReport.Load(reportPath)
    [C#]
    customerReport.Load(reportPath);
  7. Declare a DataSet and assign to it the DataSetConfiguration.CustomerDataSet property.
    [Visual Basic]
    Dim myDataSet As DataSet = DataSetConfiguration.CustomerDataSet
    [C#]
    DataSet dataSet = DataSetConfiguration.CustomerDataSet;
  8. Call the SetDataSource() method of the customerReport ReportDocument instance and pass into it the DataSet instance.
    [Visual Basic]
    customerReport.SetDataSource(myDataSet)
    [C#]
    customerReport.SetDataSource(dataSet);
  9. On the next line, beneath the report loading, bind the ReportSource property of the CrystalReportViewer to the ReportDocument instance.
    [Visual Basic]
    myCrystalReportViewer.ReportSource = customerReport
    [C#]
    crystalReportViewer.ReportSource = customerReport;

You are now ready to build and run your project.

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

  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.
    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.
  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, continue to Conclusion.

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

See Also

Tutorial: Connecting to ADO.NET DataSets | Tutorials and Sample Code | Tutorials' Sample Code Directory