You are on page 1of 5

Crystal Reports:

-It was a most popular reporting tool used by many companies for desiging Reports.

-It belongs to the company Business Objects but was integrated directly with your
Visual Studio.

-If you want to add a report under the project open "Add New Item" window & select
Reporting in the LHS panel, then select Crystal Report from RHS panel, which
creates a Report file that gets saved with .rpt extension.

-Every Report when opened has 5 sections in it:


-Report Header
-Page Header
-Details
-Report Footer
-Page Footer

-Report Header: Content placed under this section gets displayed on top of the
report i.e. on top of the first page in report.
eg: Company Name, Address etc.,

-Page Header: Content placed under this section gets displayed on top of every
page into which the report extends.
eg: Column Names

-Details: Content into this section generally comes from the DB, this is the
actual information that has to be presented to clients.
-Report Footer: This is same as Report Header but gets displayed on bottom of the
report.
eg: Signatures
-Page Footer: This is same as Page Header but gets displayed on bottom of every
page into which the report extends.
eg: Page No's.

Creating a Report:
-Open a new project of type Windows and name it as ReportProject, open the add new
item window, select Reporting on LHS & choose Crystal Report on RHS, name the
report as Bank.rpt, then choose blank report option and click Ok.

-As the report has to get it's information from DB first we need to configure the
report with an appropriate Data Source using the "Field Explorer" window that gets
added along with Crystal Report
(Ctrl + Alt + T).

Configuring the Report with DB:


-Open the Field Explorer, right click on DB Fields Node & select 'Database Expert'
option, which opens a window in it expand the Node 'Create New Connection' & double
click on the Node 'OLEDB (ADO)', that displays u list of providers, choose
'Microsoft OLEDB Provider for Sql Server' & click next there specify the connection
details like server name, user id, password & database, then Click Finish which
adds a node under OLEDB (ADO) with the name of your server, expand it, below that
it shows the DB you specified eg: CSharp7, expand it & expand the node "dbo", below
that Tables and double click on the table u want to choose, eg: Emp which adds the
table under Selected Tables list, click ok which adds the Selected Table under DB
Fields node of the Field Explorer.
Desiging the Report:
-Go into Report Header section right click on it and select Insert Text Object
which creates u an object like a Label, enter some content in it and do the
necessary alignments using the format toolbar on the top.

-Go to Page Header section right click on it and select Insert -> Special Field ->
DataDate, place it on LHS of the section, in the same way select DataTime and place
it on RHS.

-Now open Field Explorer expand the table Emp we have selected which displays the
column names below, drag and drop each column on to details section as per your
design which will create 1 Field on the Page Header (Column Name) and 1 Field on
the Details (Data).

-Now go to Report Footer & provide the option for users to enter Date, Place and
Signature using the Text Objects and Insert Line Options.

-Now go the Page Footer section right click Insert -> Special Field -> select Page
Number and place it on the Center of the section.
-----------------------------------------------------------------------------------
--
Displaying the Report:
-To display the report we were provided with a control known as
CrystalReportViewer, which has to be placed on a Form that will by default use the
Dock property as Fill.

-If you want to show the report under the control first it has to be loaded into
the application from its physical location. To load a report we can use the Class
ReportDocument which provides a method Load for loading the report from physical
location into the application.

-CrstalDecisions.CrystalReports.Engine
-ReportDocument()
-Load(string reportpath)

-Now place a CrystalReportViewer control on form and write the following code:

using CrstalDecisions.CrystalReports.Engine
-----------------------------------------------------------------------------------
-Under Form Load:
ReportDocument obj = new ReportDocument();
obj.Load("<path of the report>");
crystalReportViewer1.ReportSource = obj;

Note: to get the path of your report goto Solution Explorer, right click on the
crystal report file and select properties, copy the value under the Fullpath
property and use it under the load method.
-----------------------------------------------------------------------------------
--
Step 1: Creation of Form1
-Open a new project of type Windows and name it as SelectionReport.
-Add the reference of System.Data.OracleClient assembly to the project.
-Design the form as above and then write the following code:

using System.Data.OracleClient;
-----------------------------------------------------------------------------------
--
Declarations:
OracleConnection con;
OracleDataAdapter da;
DataSet ds;
-----------------------------------------------------------------------------------
--
con = new OracleConnection(
"User Id=Scott;Password=tiger");
da = new OracleDataAdapter("Select Empno From
Emp", con);
ds = new DataSet();
da.Fill(ds, "Emp");
comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "Empno";
-----------------------------------------------------------------------------------
--
Under ShowReport Button:
Note: Write this code after creation of Form2

Form2 f = new Form2();


f.Empno = int.Parse(comboBox1.Text);
f.ShowDialog();

Note: When u call the Show or ShowDialog method on a form it will start execution
of the form from the load event.
-----------------------------------------------------------------------------------
-
Step 2: Designing of the Report
-Add a Crystal Report under the project.

-Configure the report with the database using the Field Explorer as following:
Right Click on Database Fields -> Database Expert -> Create New Connection -> Oledb
(ADO) -> Microsoft Oledb Provider for Oracle -> Click Next Provide the Connection
Information -> Finish.

-Adds a node under Oledb(ADO) as Connection expand it which shows "Add Command"
node double click on it which opens u a window where u can write a Select Statement
on the LHS TextArea.

-Our select statement should execute basing on the selection made with in the
ComboBox of Form1 which should be sent as a parameter to the report so first we
need to create a parameter and use it under the select statement.

-To create a parameter click on the "Create" button which opens a window under
which u need to enter the following:

1. Parameter Name: Eno


2. Prompting Text: Enter Employee Number
3. Value Type: Number
4. Default Value: 7499 (optional)

-Click Ok which adds u the parameter on the RHS List Box, now on the LHS TextBox
write the following Sql Stmt:

Select E.Empno, E.Ename, E.Job, E.Mgr, E.Hiredate, E.Sal, E.Comm, D.Deptno,


D.Dname, D.Loc From Emp E Inner Join Dept D
On E.Deptno=D.Deptno Where E.Empno = {?Eno}

Click Ok -> Ok -> Ok which will add u the Command under the Database Fields, if u
expand the Command it will display the columns u mentioned under the Select Stmt.
-U can also Add Paremeters using the Field Explorer window, to create a Parameter
right click on the node Parameters and Select Add which will prompt for the Name of
the Parameter enter a name and click ok.

-Following the above process create 2 parameters CompanyName and Address, drag &
drop them on the ReportHeader, do the necessary alignments.

-Place the DataDate and DataTime Fields on the PageHeader.

-Drag and Drop the Columns under the Command into the details section and pull back
the column names from PageHeader to Details.

-Under the report footer provide the option to enter the Date, Place and Signature.

-Place the PageNumber Special Field on the PageFooter.


-----------------------------------------------------------------------------------
--
Step 3: Launching the Report
-Add a New Form under the project and place a CrystalReportViewer control on it,
add the reference of System.Configuration assembly.

-Open the Add New Item window and add the "Application Configuration File" item
which creates u the app.config file under it write the following code with in the
configuration node:
<appSettings>
<add key ="Cname" value ="NIT Technologies"/>
<add key ="Addr" value="Ameerpet, Hyderabad"/>
</appSettings>

-Write the following code under the form:

using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
-----------------------------------------------------------------------------------
--
Class Declarations:
internal int Empno;
-----------------------------------------------------------------------------------
--
Under Form2 Load:
string cname = ConfigurationManager.AppSettings.Get("Cname");
string addr = ConfigurationManager.AppSettings.Get("Addr");

ReportDocument obj = new ReportDocument();


obj.Load("<path of the report>");
crystalReportViewer1.ReportSource = obj;

//Sending values to parameters of report:


obj.SetParameterValue("CompanyName", cname);
obj.SetParameterValue("Address", addr);
obj.SetParameterValue("Eno", Empno);
-----------------------------------------------------------------------------------
-
Note: using the SetParameterValue method of the ReportDocument class we can pass
values to the Parameter Fields of the report.
SetParameterValue(string pname, object value)
SetParameterValue(int index, object value)

You might also like