Displaying .NET DataSet with Crystal Reports

February 26, 2008 at 8:36 AMAmer Gerzic

Couple of days ago, I started playing with Crystal Reports engine included with Visual Studio.NET. After creating several reports using SQL Express database, I started wondering how to create reports that require more sophisticated data analysis. At first, my thoughts were to utilize stored procedures to create report result, and and then display the result using Crystal Report engine. However, it turned out that Crystal Report engine has some limitations when it comes down to stored procedures. Crystal Reports documentation states that stored procedures can be utilized if they contain at most one SQL SELECT statement. In addition, the documentation states that no return parameters can be utilized (parameters declared by SQL keyword OUT, or INOUT). Clearly, complicated data analysis cannot be performed using single SELECT statement. Considering these limitations, I immediately started to investigate options to present a structure using Crystal Reports. At first, I considered a .NET containers, but Crystal Reports engine did not seem to provide any convenient way of displaying such structures. In addition, I noticed that every report that I designed, required the structure of the data to be known at report design time. At that time, two options came to my mind:

  1. Create a .NET class, that would contain the data and utilize it in report design;
  2. Create a .NET DataSet using Visual Studio.NET engine; In this way, the DataSet structure can be defined during design time, and hence utilized in the report;

After some considerations, I decided to investigate the second option first. First option will be discussed in an entry later!

To utilizes second option, first we need to create a data set. To add DataSet in VS.NET please perform following steps:

  1. In solution explorer, right-click on your project and select Add - New Item
  2. From Add New Item dialog box, select DataSet
  3. Once the DataSet is added, DataSet editor will be open. At this point we are ready to structure our data any way we want. We'll make it simple and add only single table;
  4. From the Toolbox, drag and drop DataTable object;
  5. Add necessary columns to DataTable object. For simplicity, we'll add only single column of type string;

At this point, our data structure is done. Following steps need to be performed to design necessary report:

  1. In solution explorer, right-click on your project and select Add - New Item
  2. From Add New Item dialog select Crystal Report
  3. A report will be added and report wizard will start. At this point make sure that as data source following is selected: Project Data - ADO.NET DataSets - TestApp.DataSet1 - DataTable1
  4. Add Column1 to the list of fields to be displayed.

At this point we are ready to display the data using our newly created report. To do so, we have to populate the DataSet with some data. Following code explains the rest of necessary steps to display the data on the report:

CrystalReport1 Report = new CrystalReport1();
DataSet1 ds = new DataSet1();
DataSet1.DataTable1DataTable t = (DataSet1.DataTable1DataTable)ds.Tables[0]; 

t.AddDataTable1Row("Row 1");
t.AddDataTable1Row("Row 2");
t.AddDataTable1Row("Row 3");
t.AddDataTable1Row("Row 4");
t.AddDataTable1Row("Row 5"); 


crystalReportViewer1.ReportSource = Report;

Investigating SetDataSource(...) method of the ReportDocument class, reveals that it is possible to display any IEnumerable object using Crystal Report engine, which will be covered in the next post.

Posted in: .NET | ASP.NET | C# | Crystal Reports

Tags: , ,

Add comment