Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Crystal Report 8.5 for Visual Studio.

NET (using C#)

By Roshni

Any Software Application designed to work with a database to analyze and interpret information
will need some kind of a report. There are tools, which help the programmer to create Reports
easily.

The Visual Studio.NET Integrated Development Environment comes with a reporting tool,
Crystal Reports 8.5 for .NET. Crystal Reports has been a powerful windows reporting tool for a
while now. So Microsoft chose that as the integrated reporting tool for .NET. Crystal Reports
can be hosted in both Web and Windows forms or can be published as a Report Web Service
on a Web server. It provides developers with data visualization and analysis capabilities through
seamless integration.

Crystal Reports can use different kinds of data sources and can be used to create different
types of reports like charts; cross-tab reports which use formulas, sub-reporting and conditional
formatting to provide easier understanding of data for the user.

Another advantage of using Crystal reports is that you dont have to install any additional
software to develop your reports and you dont need to distribute the report designer with your
report to the client makes the programmers life easier.

Run time customization is possible using the web or windows forms viewer. Which means users
can view different reports, change the format, data selection, or export options of an existing
report as the viewer control can interact with other controls in the same page. For Ex. By
clicking a button user can sort a report based on some a selected criteria.

The Crystal Report viewer control behaves differently in Windows forms and Web forms

Working with Crystal Reports in VS.NET has two phases.


1. Working with Crystal Report
2. Connecting Crystal Report with Visual Studio.NET
Working with Crystal Report

Creating Crystal Reports mainly has the following steps


Open a report file in VS.NET
Create a new project or in the existing project select add new item in the file menu or by right
clicking the project name

1
Select Crystal report from the list of icons and name the report and click Open

1. It takes you to the crystal report gallery dialog box.


2. Open a blank report

2
Add a Data source for report
If you dont see the Field Explorer window on the screen. Select View-Other windows-
Document Outline from the Menu bar

1. Right click database items select ADD/Remove


Database

Select a data source (you can select any existing dataset in your project or a stored procedure
or a table etc.) You will need to provide the login information for the database

3
You select the required Stored Procedure or table or dataset and add to the report

Select the fields to be shown in the report and put them in the proper sections of the report

Mainly a report consists of five sections.

Report Header

4
Page header
Detail
Page footer
Report footer.

The main report objects are

Database fields
Formula fields
Parameter fields
Group Name fields
Running Total fields
Summary fields

Depending upon your requirements you place objects in the corresponding sections. You can
insert text boxes and special fields like date/time. Also you can insert unbound fields, which can
be connected (bound) to database during runtime

Set grouping, totaling and sorting if needed. Depending on the requirements you may have to
use formulas and sub reports also.
For inserting fields or grouping, sorting etc. you right click on the report and select the action
you want.

5
Formatting the report
You can set the font style and put images also. To format any object you right click on in and
select the format option.
2. Interacting with Visual Studio.NET
To view the report in VS.NET you need to do the following steps.
Add a CrystalReportViewer control to the form and customize the properties
The VS.NET tool bar has the CrystalReportViewer control. Drag and drop it in the form where
you need to view the report. ( if you dont want to show the report to the user you dont have to
use a viewer control.).
There is a tool bar in the viewer which has buttons for navigation, refresh etc. you can view or
hide them using the property window at design time.
Set the report source of the CRV. There are three ways to do this:
To set the Report Source property of a CrystalViewer control by report name:

Select CRV1.

1. Click on properties window


2. Select ReportSource property click browse and select the report you want to set as the
source.
3. Alternatively in the code you can set in the pageload or button click event

CRV1.ReportSource = "path of report file"; ("C:\\folder\\subfolder\\file.rpt")


Be sure to place double forward slashes in the path.

To set the Report Source property of a CrystalViewer control by report document object:

Add reference to CrystalDecisions.CrystalReports.Engine.


(ReportDocument Class is under this package)

Create ReportDocument object.

Load the report into it using ReporDocument1.Load() method.

6
Assign ReportDocument1 as the report source of CRV1

To set the Report Source property of a CrystalViewer control by local report object:

When the report to be displayed is created under the same project you can directly create an
object of that report and set it as the report source

CrystalReport1 rep = new CrystalReport1();


CRV1.ReportSource = rep;

Passing Logon Information to Crystal Viewer

When you use a tables or stored procedures as the data source of the report you will have to
pass the logon information to the report object in the code. The Code for that is as showed here.
The variable 'rep' is an object of the Crystal report to be displayed

CrystalReport1 rep = new CrystalReport1();


TableLogOnInfo logOnInfo = new TableLogOnInfo();
logOnInfo =rep.Database.Tables[0].LogOnInfo;
ConnectionInfo connectionInfo = new ConnectionInfo ();
connectionInfo = logOnInfo.ConnectionInfo;
connectionInfo.DatabaseName = "Database";
connectionInfo.ServerName = "Server";
connectionInfo.Password = "pwd";
connectionInfo.UserID = "uid";
rep.Database.Tables [0].ApplyLogOnInfo(logOnInfo);

4. Passing Parameters to Crystal Viewer

Also when the report is made based on the parameter input from the user the inputs are passed
to the report as parameters for the selection formula or Stored proc behind the report

The code is as following

ChecksCRV.ReportSource = rep;
ParameterFields crParameterFields = new ParameterFields();
ParameterField crParameterField = new ParameterField();
ParameterDiscreteValue crParameterDiscreteValue = new
ParameterDiscreteValue();
crParameterFields = ChecksCRV.ParameterFieldInfo;
crParameterField = crParameterFields[0];
crParameterDiscreteValue.Value = Param1
crParameterField.CurrentValues.Add(crParameterDiscreteValue);
ChecksCRV.ParameterFieldInfo = crParameterFields;
ChecksCRV.ReportSource = rep;

The following code is used to pass parameters to the reportobject at runtime

ParameterValues paramValues = new ParameterValues();


ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue
();

paramDiscreteValue.Value = paramValue;
paramValues.Add (paramDiscreteValue);
rep.DataDefinition.ParameterFields["@param1"].ApplyCurrentValues(AddDi
screteValue(param1));

7
These are the basic steps the developer has to follow in order to show a report in a .NET
application.

Helpful Links on Crystal Reports 8.5 in .NET

http://msdn.microsoft.com/library/?url=/library/en-us/crystlmn/html/crconcrystalreports.asp?frame=true
http://www.crystaldecisions.com/products/dev_zone/net_walkthroughs.asp
http://dotnet247.com/247reference/msgs/g/ngfx-crystal.aspx
http://support.crystaldecisions.com/library/kbase.asp
http://support.crystaldecisions.com/forums/

You might also like