Microsoft.NET

……………………………………………….Expertise in .NET Technologies

An Overview of Crystal Reports

Posted by Ravi Varma Thumati on May 6, 2009

Article Contents:

  • Overview
  • Introduction
  • Getting a Feel of It – Using an Existing Crystal Report File in Your .aspx Page

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. Please note that we have tested the below given sample code illustrations with the beta 2 version of Visual Studio .NET only.

The topics that we have covered here are:

1) Introduction

2) Getting a feel of it – Using an existing Crystal Report file in your .aspx page

Introduction 

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.

—- Advantages —–

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 Architecture —-

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

The first step to implement a report into your web application would be to create it using the Crystal Report 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.

—- Implementation Models —–

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.

—- Report Types —-

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 .vb 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.

—- Other things you should know —-

–         Though the Crystal Report Viewer control comes with some cool in-built options like zooming, page navigation, etc., it does not have a custom print option. You will have to depend on the browser’s print feature.

–         An un-registered copy of Crystal Report for Visual Studio .NET will remain active only for the first 30 uses, after which the ‘save’ option will be disabled.  To avoid this, all you have to do is register the product with www.crystaldecisions.com for which you are not charged.

–         The default installation will service only 5 concurrent users. To support more users, you will have to buy additional licenses from http://www.crystalDecisions.com

Getting a Feel of It – Using an Existing Crystal Report File in Your .aspx Page

Let’s take a look at how we could get this done the fast way to get a feel of how a Crystal Report file would look like in your web form.

1) Drag and drop the “Crystal Report Viewer” from the web forms tool box on to the .aspx page

crystal1 

2) Bring up the properties window for the Crystal Report Viewer control

3) Click on the […] next to the “Data Binding” Property and bring up the data binding pop-up window

4) Select “Report Source” from the “Bindable properties” section on the left side

5) Select the “Custom Binding Expression” radio button, on the right side bottom of the window and specify the sample .rpt filename and path as

“C:\Program Files\Microsoft Visual Studio.NET\Crystal Reports\Samples\Reports\General Business\World Sales Report.rpt”(including the double quotes) and Click “ok”

crystal2 

Note: The ‘World Sales Report.rpt’ file is created as a part of Visual Studio .NET installation. If you have specified a different directory during installation then make necessary changes to the above specified path.

In a couple of seconds you should see the Report Viewer Control load a preview of the actual report during design time itself. The reason for the data being loaded during design time is that the report has been saved with the data.

The above steps actually insert the following code into your .aspx page :

<%@ Register TagPrefix=”cr” Namespace=”CrystalDecisions.Web” Assembly=”CrystalDecisions.Web” %>

above the Page Directive and

<CR:CrystalReportViewer

id=”CrystalReportViewer1″ runat=”server”

Width=”350px” Height=”50px”

ReportSource='<%# “C:\Program Files\Microsoft Visual Studio.NET\Crystal Reports\Samples\Reports\General Business\World Sales Report.rpt” %>’>

</CR:CrystalReportViewer>

within the <FORM> section of the page.

6) Call the DataBind method, on the Page Load Event of the Code Behind file (.aspx.vb).

  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)       

DataBind()

End Sub

7) Save, build and run your page.

There!!! You have a web form page with a Crystal Report file embedded into it.

crystal3

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: