Monday, June 25, 2007

Crystal Report for Visual Studio .NET: ASP Alliance

Crystal Report for Visual Studio .NET: ASP Alliance

An overview of Crystal Reports. Walkthroughs show how to use both Pull and Push methods as well as exporting the report file into other formats.

by Eric LandesFeedback
Average Rating: Views (Total / Last 10 Days): 349114/ 2925
Article Contents:
Overview
Introduction
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
Overview
[ Back To Top ]
Editor's Note: The first edition of this article was written by Ajay Varghese.
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. 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
3) Crystal Reports Walkthrough - using the Pull Model
4) Crystal Reports Walkthrough - using the Push Model
5) Exporting the Report file into other formats
Introduction
[ Back To Top ]
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 http://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
[ Back To Top ]
Lets 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

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"

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 :

above the Page Directive and
' Height="50px" Width="350px" runat="server">

within the
section of the page.
6) Call the DataBind method, on the Page Load Event of the Code Behind file (.aspx.vb).
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.
Crystal Reports Walkthrough - Using the Pull Model
[ Back To Top ]
We would be using the following steps to implement Crystal Reports using the Pull Model :
1. Create the .rpt file (from scratch) and set the necessary database connections using the Crystal Report Designer interface.
2. Place a CrystalReportViewer control on the .aspx page and set its properties to point to the .rpt file that we created in the previous step.
3. Call the databind method from your code behind page.
Creating the .rpt File:
1) Add a new Crystal Report to the web form by right clicking on the "Solution Explorer", selecting "Add" --> "Add New Item" --> "Crystal Report".

2) On the "Crystal Report Gallery" pop up, select the "As a Blank Report" radio button and click "ok".


3)This should open up the Report File in the Crystal Report Designer


4) Right click on the "Details Section" of the report, and select "Database" -> "Add/Remove Database"

5) In the "Database Expert" pop up window, expand the "OLE DB (ADO)" option which should bring up another "OLE DB (ADO)" pop up

6) In the "OLE DB (ADO)" pop up, Select "Microsoft OLE DB Provider for SQL Server" and click "Next"


7) Specify the connection information
Server : HomePC (Make use of your server name here)
User Id : sa
Password :
Database : Pubs

8) Click "Next" and then click "Finish"

9) Now you should be able to see the Database Expert showing the table that have been selected

10) Expand the "Pubs" database, expand the "Tables", select the "Stores" table and click on ">" to include it into the "Selected Tables" section.


11) Now the Field Explorer should show you the selected table and its fields under the "Database Fields" section, in the left window.

12) Drag and drop the required fields into the "Details" section of the report. The field names would automatically appear in the "Page


www.svdeals.com

No comments: