Click here to Skip to main content
Click here to Skip to main content

Crystal Reports using C#.NET 2003 Windows Application

, 25 Jul 2006
Rate this:
Please Sign up or sign in to vote.
Crystal Reports for using C#.NET 2003 Windows Application (From Scratch to Deployment)

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.

 

 Sample screenshot

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.

 

Sample screenshot 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.

 

 

Sample screenshot 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

    private void SetLogonInfo()

    {

         try

         {

LogInfo.ConnectionInfo.ServerName=”SerVerName”;

LogInfo.ConnectionInfo.UserID=”sa”;

LogInfo.ConnectionInfo.Password=”ok”

LogInfo.ConnectionInfo.DatabaseName=”CrystalSample”;

        }

       catch(Exception ex)

        {

         MessageBox.Show(ex.Message);

        }       

    }

 

 

 

Now we need to create  object of the report. There are various ways to show report.

 

a)      Create one object of Report Document that can be use to every report which are going to be shown on that CrystalreportViewer can be created as

            ReportDocument O_Report=new ReportDocument();

    We can use this object to every report and need to give path while loading the report.as

      O_Report.Load(@"E:\CrytstalSample\rptShowList.rpt");

   

b)    Create different objects for different reports as

 

     rptShowList O_showList = new rptShowList();

 

(Here I am using second option).

 

Make DataBase Connection as

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

 

Step 6:-

        Now I want to show AddressList of the users whose number is greater than 15. For that I will fill DataTable with these records and then attach it to the report.

 

DataTable DT = new DataTable();

       try

           {

              string CmdStr="";

              CmdStr = “Select * from Vw_AddressList where User_Number>15”;

               Connection.Open();             

              SqlDataAdapter Adpt=new SqlDataAdapter(CmdStr,Connection);

              DT.Clear();

              Adpt.Fill(DT);             

           }

           catch(Exception ex)

           {

              MessageBox.Show(ex.Message);             

           }

           finally

           {

              Connection.Close();

        }        

   

 

Step 7:-

    Now set the above filled DataTable as Reports dataSource.

 

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

 

Step 8: -

    Set the report as crystal report viewer report source to bind report to viewer. 

CryStalReportViewer1.ReportSource = O_Report1111;

CryStalReportViewer1.Zoom(1);

 

How to Change the properties of Fields on report programmatically.

 

 The Fields on the Detail section,Formula Fields,group Name Fields are the FieldObjects. We can change it’s properties like font,size programmatically.

Get the name from the properties window & create object of that field.get the section number & change it’s properties.

 

e.g. In the above Report User Number and user Streetasddress are the field objects. We can change there properties as

 

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

 

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

 

Same can be done with Text Object.

 

 

We can change properties of Formula Field in different way also.

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

 

 

Formula Fields:-

 

  Here I will just give sample of how to use formula field. It’s out of scope to give whole Formula Field Description.

 

In my database I have stored Gender field as “M” or “F” but in report I want to show it as “Male”, “Female” etc.

 

·         Right Click on Formula Field -> New

·         Give Formula Name.

The Formula Editor screen will appear as follows-

Sample screenshot 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The left side Section contains database fields there we can select on which field we want to write formula. Middle section is Functions. We can apply different types of functions to formula. The Right section is of Operators where we use operators in formula.

 

Now I have written code as –

 

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

'Male'

else

'Female'

 

 

 

 

 

 

Deploying the Crystal Report Dot Net Application:

 

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

Sample screenshot 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Select SetUp and Deployment Projects -> Setup Project.

Give name to your Setup.

 

Step 2:- To add Exe and other files-

       

    In File System, Right click on Appllication Folder (It creates folder at Win/Sys path generally) -> Add -> Project Output -> select  Primary output & other needed files.

    Up to this is general Deployment process. If your application includes Crystal Reports you need to do some extra.

 

a)    Adding Merge Module:-

·         Right Click on Setup project -> Add -> Merge Module

·         Select ‘Crystal_regwiz2003.msm’, Crystal_Managed2003.msm,           Crystal_Database_Access2003_enu.msm, Crystal_Managed2003.msm

·         Click on Open.

 

b)      Now important thing is that When you run this Set up on another sytem it will give you error  of “Keycode32.Dll”

The error is due to not giving Registration key to Merge Module File       ‘Crystal_regwiz2003.msm’ To do that Right Click on  ‘Crystal_regwiz2003.msm’ and select Properties.

    In properties select ‘(MergeModuleProperties)’ ->

 Enter your key at ‘License Key‘.

Now the License key can be your Crystal Report product key or you can get it from,

Help(Menu of dot net) -> About Microsoft Development Environment.

There you will see the string “Crystal Reports for Visual Studio .NET AAP50-GS00000-U7000RN“(can be different for different versions).Your key is 'AAP50-GS00000-U7000RN'.

 

Now build the solution & your set up is ready.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Hrushikesh Mokashi
Web Developer
India India
No Biography provided

Comments and Discussions

 
Questioncrustal reports PinmemberpanduRajju5-Apr-12 18:53 
GeneralFormula Fields Pinmemberlonghn5-Nov-08 22:47 
Questionhow change font at runtime Pinmemberkianoosh soorani3-Nov-07 4:07 
Generalformulae fields Pinmembersuparichit3-Sep-07 1:22 
GeneralRe: formulae fields PinmemberHrushikesh Mokashi20-Sep-07 21:38 
GeneralCrystal Reports Problem Pinmemberkhuhro31-Aug-07 5:53 
GeneralRe: Crystal Reports Problem PinmemberHrushikesh Mokashi20-Sep-07 21:40 
GeneralRe: Crystal Reports Problem Pinmembergaurav.ipec29-Jul-10 0:49 
QuestionCombine a Database field in a text object programmatically ? Pinmemberledz25-Jul-07 2:43 
Generalthanks Pinmembernadeem111as11-Jun-07 6:21 
Questioncan anybody send dynamic crystal report in windows Pinmembermadhuoo30-Mar-07 23:30 
QuestionProgressBar while loading Report PinmembersDisco1-Mar-07 1:32 
GeneralChanging database type at run time PinmemberAlexEvans28-Nov-06 16:07 
GeneralAssigning the dataset only works for one table PinmemberJan Schreuder2-Aug-06 2:47 
Questionhow to view the contents in crystal reports using dataset generated at run time Pinmemberpuneetaggarwal1629-Jul-06 22:06 
GeneralRe: how to view the contents in crystal reports using dataset generated at run time [modified] PinmemberJan Schreuder1-Aug-06 10:50 
GeneralRe: how to view the contents in crystal reports using dataset generated at run time PinmemberJan Schreuder2-Aug-06 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 26 Jul 2006
Article Copyright 2006 by Hrushikesh Mokashi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid