Big Data

Creating reports with Visual Studio .NET has never been easier

In application development, the ability to generate informative reports is imperative to the overall success of the project. With the incorporation of Crystal Reports in VS.NET, you have the basic tools needed to create such reports.

By Tony Patton

Data collection and management are tasks common to most applications, but the data amassed over time is useless on its own. It needs to be organized or summarized to be useful to management personnel. With this in mind, management most often demands reports be generated to monitor activities like customer sales.

Providing the necessary reporting structure can be as easy as creating a simple Web form or as complicated as utilizing a third-party solution such as Crystal Reports. Crystal Reports is available as a stand-alone product with countless options, but .NET includes a standard version (Crystal Reports 8.5) to meet common requests. Here, I will provide a basic introduction to using Crystal Reports in .NET development.

Crystal Reports option
You can easily add a report to an application via .NET and Crystal Reports. Figure A shows a Crystal Report as the selected item in the .NET project’s Add Item dialog box.

Figure A
Adding Crystal Report to project

This places the Crystal Report component in the project’s Solution Explorer pane.

Figure B
Choosing Crystal Report type

The Crystal Report Gallery (see Figure B) dialog box follows the addition of a Crystal Report. It allows you to create a report document with the following options:
  • Using The Report Export—The new report is created using a wizard to choose data source, formatting, and so forth.
  • As A Blank Report—The report is created blank, so you can choose report layout.
  • From An Existing Report—An existing report (i.e., .rpt file) is used as the template for the new report.

The document option you choose depends upon your situation, but the Report Export is a good choice the first time since the result will provide a skeleton for future work with reports. In addition, the Choose an Expert option allows you to choose the report’s display format when using the Report Export. The options are:
  • Standard
  • Form
  • Form Letter
  • Crosstab
  • Subreport
  • Mail Label
  • Drilldown

The expert is your guide
The Report Export allows you to quickly create a report by pointing and clicking. It begins by selecting Report Export and the Choose An Expert option. For example purposes, I chose the Standard Report option. This will display the dialog box shown in Figure C.

Figure C
Standard Report options

The numerous tabs in Figure C allow you to specify the data and what aspects of that data are used in the report. The following is a rundown of the tabs:
  • Data—The report data source is selected; specify the table(s).
  • Fields—The fields are chosen from the selected data source(s).
  • Group—Signal if the report data is grouped and, if so, what field(s) to use.
  • Total—Specify any totals used on the report.
  • Top N—This option can be used to specify that only a certain number of items are displayed when the Group By option is used.
  • Chart—Adds a chart to the report.
  • Select—Edits the selection criteria.
  • Style—This allows you to choose the report style and display the format.

These options occur in order. The Data tab begins the process by specifying the report data source. Taking advantage of an ADO.NET connection requires that you create that connection before creating the report. It will then be available on the Data tab. You have the option of adding a Dataset item to the project. Figure D shows how to do it by opening the Add New Item dialog and selecting the Dataset item.

Figure D
You can add the data source to the project.

Once these options are specified, the new report object is added to the project. It is stored in a file with the .rpt file extension. This report object may be used on other items, such as a Web or Windows form.

Viewing the report
Including a report on a form is as easy as dragging and dropping the necessary components. First, a report document must be added to the form, which is accessible by way of the Visual Studio Toolbox on the Components tab (see Figure E).

Figure E
Add the report to the form.

This is followed by the dialog box (shown in Figure F) where you actually select the Crystal Report.

Figure F
Specify which report should be added.

Next, you must add the Crystal Report viewer to the form, so the report can be displayed accordingly. Figure G shows the addition of the viewer; you can access it from the Web Forms tab in the Visual Studio toolbox.

Figure G
Add the viewer to the form.

Once the viewer is on the form, the previously added report document (Figure H) is assigned to the viewer via its properties (which you can access by right-clicking on the viewer). The viewer properties are accessed via the Databindings property of the viewer.

Figure H
Assign the report to the viewer.

Making the job of reporting easier
Reporting is an essential aspect of application development. System users need to keep an on the data to monitor activity. Visual Studio .NET provides the necessary tool to create such reports with Crystal Reports.

Editor's Picks

Free Newsletters, In your Inbox