You are on page 1of 11

Step 1 :-

To open the crystal report document, Right Click on your project.


Select Add -> Add New Item. New window will appear as below. Give Name to your
report.
Step 2:-
After clicking Open u will see another window giving you options how do you want to
create the report, using Report Expert or from Blank Report or from an existing report.
Here I am selecting second option i.e. As a Blank Report.
Now your Crystal Report Document will get opened. On the left hand side there you will
see Field Explorer from where you can interact with database.
(If Field Explorer is not there then to open it you can use shortcut key Ctrl + Alt + T)

Step 3:-
Now to select database, (My Database is stored in SQL Server 2000)
Right Click on Database Fields in Field Explorer.
Select Add/Remove Database -> OLEDB(ADO) ->Make New Connection -> Microsoft
OLE DB Provider for SQL Server (As I am using SQL Server 2000). Click on Next and
give your connection Information, Click on Next and then Click on Finish.

Now you can add your table/s, view/s which you required for the Report. The window
looks like this.
Step 4: -
Now in your Field Explorer you can see your chosen Table/View. Drag the Fields you
want to show in the report in Details Section. The screen will appear as follows.
The fields in Details section are Field Objects and the fields in Page Header, Report
Header are Text Objects.

Step 5:-
Now to show this report, Take a form & add a CrystalReportViewer to it.(Just drag
CrystalReportViewer from Tool box on the form).

For crystal reports add name spaces are important

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

(Here I am showing report on Page Load, same code can be written on


Button Click if wanted.)

Now even if we have connected database to report but if we changed


database then it is not feasible to create new crystal reports again &
again so we can use TableLogOnInfo to connect report to Database
programmatically.
Create Object of that as
private TableLogOnInfo LogInfo = new TableLogOnInfo();

The function can be written as

privatevoid SetLogonInfo()
{
try
{
LogInfo.ConnectionInfo.ServerName=”SerVerName”;
LogInfo.ConnectionInfo.UserID=”sa”;
LogInfo.ConnectionInfo.Password=”ok”
LogInfo.ConnectionInfo.DatabaseName=”CrystalSample”;
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}

Now we need to create object of the report. There are various ways to showreport.
a) Create one object of Report Documentthat can beuse to every report which are
going to be shown on that CrystalreportViewer canbe created as
ReportDocumentO_Report=new ReportDocument();
We can use this object to every report and need togive pathwhile
loading the report.as
O_Report.Load(@"E:\CrytstalSample\rptShowList.rpt");

b) Createdifferent objects for different reportsas

rptShowList O_showList =new rptShowList();

(Here I am using secondoption).

Make DataBase Connectionas


O_Report1.Database.Tables[0].ApplyLogOnInfo(LogInfo);

Step 6:-
Now I want to show AddressList of the userswhose number is
greater than 15. For that I will fill DataTable with theserecords
and then attach it to the report.

DataTable DT = new DataTable();


try
{
stringCmdStr="";
CmdStr = “Select * from
Vw_AddressListwhereUser_Number>15”;
Connection.Open();
SqlDataAdapter Adpt=newSqlDataAdapter(CmdStr,Connection);
DT.Clear();
Adpt.Fill(DT);
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
finally
{
Connection.Close();
}

Step 7:-
Now set the above filled DataTable as ReportsdataSource.

O_showlist.Database.Tables[0].SetDataSource(DT);

Step 8: -
Set the report as crystal report viewer reportsource tobind
report to viewer.
CryStalReportViewer1.ReportSource = O_Report1111;
CryStalReportViewer1.Zoom(1);

How to Change the propertiesof Fields on report programmatically.


The Fields on the Detail section,Formula Fields,groupName Fieldsare
the FieldObjects. We can change it’s propertieslike
font,sizeprogrammatically.
Get the name from the properties window&createobject of thatfield.get
the section number&change it’s properties.

e.g. In the above ReportUser Number and user Streetasddress are the
field objects. We can change thereproperties as

FieldObject fUserNumber
=FieldObject)o_showlist.Sections3.ReportObjects["Field1"];

fFullName1.ApplyFont(new Font(Arial,8.25F));

Same can be done withText Object.

We can change properties ofFormula Fieldin different way also.


O_showlist.DataDefinition.FormulaFields["Header"].Text= “Address List”;

Formula Fields:-

Here I will just give sample of how to use formulafield. It’s out
ofscope to give whole FormulaField Description.

In my database I have storedGender field as “M” or “F” but in report I


want to show it as “Male”, “Female”etc.

• Right Clickon Formula Field ->New


• GiveFormula Name.
The Formula Editor screenwill appear as follows-
The left side Sectioncontains database fields there we can
select on which field we want to writeformula. Middle section
is Functions. We can apply different types of functionsto
formula. The Right section is of Operators where we use
operators informula.

Now I have written code as–

if({Vw_AddressList.Gender})='M' then
'Male'
else
'Female'

Deploying theCrystalReport Dot NetApplication:

Step 1:-Right click on Solution->Add New Project


Select SetUp and DeploymentProjects ->Setup Project.
Give name to yourSetup.

Step 2:-To add Exe and otherfiles-

In File System, Right click on Appllication Folder(Itcreatesfolder


at Win/Sys path generally) ->Add ->ProjectOutput ->select Primary
output&other needed files.
Up to this is general Deployment process. If
yourapplicationincludes Crystal Reports you need todo some extra.

a) AddingMerge Module:-
• Right Clickon Setup project ->Add ->Merge Module
• Select‘Crystal_regwiz2003.msm’, Crystal_Managed2003.msm,
Crystal_Database_Access2003_enu.msm,Crystal_Managed2003.msm
• Click on Open.

b) Nowimportant thing is that When you run this Set up on another sytem it will
giveyou error of “Keycode32.Dll”
The error isdue to not giving Registration key to Merge Module File‘
‘Crystal_regwiz2003.msm’ Todo that Right Click on
‘Crystal_regwiz2003.msm’ and selectProperties.
In properties select ‘(MergeModuleProperties)’->
Enter your key at‘License Key‘.
Now the License key can beyour Crystal Report product key or you can
get it from,
Help(Menu of dot net) ->About Microsoft Development Environment.
There you will see thestring “Crystal Reports for Visual Studio .NET
AAP50-GS00000-U7000RN“(can bedifferent for different versions).Your
key is'AAP50-GS00000-U7000RN'.

Now build the solution&your set up is ready.


About Hrushikesh Mokashi

You might also like