Before we started off this small research on Crystal Reports for Visual Studio .NET, my friend and I were inquisitive about the complexity with regard to its implementation into our web application. A week later, with a lot of effort going into hunting for the ‘how-to’ documentation online, we have managed to integrate some simple reports into our ASP.NET application and try some neat tricks with it!!
This article is a compilation of required material to kick-start the process of implementing Crystal Reports into your .NET web application and should reduce your frustrating efforts (spent for the same research that we made) to a trifle by using these step-by-step walkthroughs. To get the best out of this article, the reader should have a basic Knowledge of database connections in ASP.NET and use Visual Studio .NET for the development.
The topics that we have covered here are :
- Getting a feel of it – Using an existing Crystal Report file in your .aspx page
- Crystal Reports Walkthrough – using the Pull Model
- Crystal Reports Walkthrough – using the Push Model
- Exporting the Report file into other formats
Crystal Report comes in various flavors and the one that is required for building reports for .NET is “Crystal Report for Visual Studio .NET”. It exposes a rich programming model with which we could manipulate its properties and methods during runtime. If you are developing your .NET applications using Visual Studio .NET then you won’t have to install any additional software as it is already built into Visual Studio .NET.
Note:Visual Studio 2010 requires to download Crystal Report software
Some of the major advantages of using Crystal Report for Visual Studio .NET are :
- Rapid report development
- Can extend it to complicated reports with interactive charts
- Exposes a report object model using which it can interact with other controls on the web form
- Can programmatically export the reports into widely used formats like .pdf, .doc, .xls, .html and .rtf
The various components that make up a simple implementation of Crystal Report as a 2-tier architecture, required for web applications are
- The Client :The client only needs a browser to access the reports which are embedded into the .aspx pages.
- The Web Server hosts the :
- Crystal Report Engine (CREngine.dll) Along with other tasks like merging the data with the report file, exporting reports into different formats, etc., it is the Report Engine that converts your Crystal Report into plain HTML that is passed on to your .aspx page.
- Crystal Report Designer (CRDesigner.dll) The reports are created from scratch using the Crystal Report Designer, with which you could design the titles,insert data, formulas, charts, sub-reports, etc.
- The .rpt Report file Designer interface You will find some ready-made .rpt samples provided with the default installation.
- The Data Source The way your .rpt file gets the data depends on which method you choose. You can choose to make Crystal Report itself to fetch your data without writing any code for it or you can choose to manually populate a dataset and pass it on to the report file. We will look at the various possibilities a little later in this article.
- Crystal Report Viewer web form Control (CRWebFormViewer.dll) The Crystal Report Viewer control is a web form control that can be inserted into your .aspx page. It can be thought of as a container that hosts the report on the .aspx page.
Note : In a more complex implementation, the reporting server and the web server could be on different physical servers, where the web server would make an HTTP request to the reporting server.The Crystal Reports could also be implemented as a web service.
Fetching the data for the Crystal Report could be done by using any of the following methods :
- Pull Model : where in Crystal Report handles the connection to the database using the specified driver and populates the report with the data, when requested.
- Push Model : where it is the developer who has to write code to handle the connection and populate the dataset, and pass it on to the report. The performance can be optimized in this manner by using connection sharing and manually limiting the number of records that are passed on to the report.
Crystal Report Designer can load reports that are included into the project as well as those that are independent of the project.
- Strongly-typed Report : When you add a report file into the project, it becomes a ‘strongly-typed’ report. In this case, you will have the advantage of directly creating an instance of the report object, which could reduce a few lines of code, and caching it to improve performance. The related .cs file, which is hidden, can be viewed using the editor’s ‘show all files’ icon in the Solution Explorer.
- Un-Typed Report : Those reports that are not included into the project are ‘un-typed’ reports. In this case, you will have to create an instance of the Crystal Report Engine’s ‘ReportDocument’ object and manually load the report into it.