Share via


Tutorial: Connecting to ADO.NET DataSets

Introduction

In this tutorial, you learn how to connect a Crystal report to an ADO.NET DataSet, through a DataSet schema. Some extra steps are required to report from an ADO.NET DataSet, because the report is not connected directly to a database.

An ADO.NET DataSet schema provides a template of the data structure in XML. However, a report cannot retrieve data from the DataSet schema alone. The DataSet schema must first be instantiated as a strongly-typed DataSet instance or as a generic DataSet instance. Then, the DataSet instance must be filled with data, through use of the DataAdapter classes.

In this tutorial, because of the added complexity that is involved in reporting from ADO.NET DataSets, the processes that you follow to create the schema and populate the DataSet are carefully kept separate from the Crystal Reports binding code.

Note

Crystal Reports for Visual Studio provides a template, named DataSet, to create an ADO.NET DataSet schema. In Web SItes created with Visual Studio 2005 and later, the DataSet template is not accessible from the Add New Item dialog box. Only the default "XML Schema" template is available. However, with minor adjustments in the code (explained in the steps that follow), you can use the default template XML Schema to achieve the same results.

To begin, you create a data connection and build a DataSet schema that is based on that connection. You then create a helper class with a property that returns a populated instance of the DataSet. Finally, you write code that binds your Crystal report to the DataSet.

For the Crystal report binding code, you write code to do the following:

  • Instantiate the report.
  • Set its SetDataSource property to the populated DataSet property from the helper class.
  • Bind the populated Crystal report to the CrystalReportViewer control.

Finally, if you are building a Web Site, you place your populated DataSet instance into an ASP.NET Cache object, to improve performance and scalability.

It is recommended that you keep the DataSet configuration process and the Crystal report binding process distinct, both to maintain clarity in the code when you bind the report, and to promote reusability of the DataSet across your Web or Windows project.

Sample Code

This tutorial comes with Visual Basic and C# sample code that show the completed version of the project. Follow the instructions in this tutorial to create a new project or open the sample code project to work from a completed version.

The sample code is stored in folders that are categorized by language and project type. The folder names for each sample code version are as follows:

  • C# Web Site: CS_Web_Data_DataSets
  • C# Windows project: CS_Win_Data_DataSets
  • Visual Basic Web Site: VB_Web_Data_DataSets
  • Visual Basic Windows project: VB_Win_Data_DataSets

To locate the folders that contain these samples, see Tutorials' Sample Code Directory.

In this section: