Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Windows

Crystal Reports using C#.NET 2003 Windows Application

3.29/5 (26 votes)
25 Jul 20067 min read 1   2  
Crystal Reports for using C#.NET 2003 Windows Application (From Scratch to Deployment)

Step 1 :- <o:p>

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.

<o:p> 

<o:p> Sample screenshot

<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"><o:lock v:ext="edit" aspectratio="t">

<o:p> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Step 2:- <o:p>

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.<o:p>

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)

<o:p> 

Step 3:-<o:p>

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.<o:p>

<o:p> 

Now you can add your table/s, view/s which you required for the Report. The window looks like this.

<o:p> 

<o:p>Sample screenshot 

<o:p> 

<o:p> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Step 4: - <o:p>

 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.

<o:p> 

<o:p> 

<o:p>Sample screenshot 

<o:p> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The fields in Details section are Field Objects and the fields in Page Header, Report Header are Text Objects.

<o:p> 

<o:p> 

Step 5:- <o:p>

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

<o:p> 

For crystal reports add name spaces are important

<o:p> 

using CrystalDecisions.Shared;<o:p>

using CrystalDecisions.CrystalReports.Engine;<o:p>

<o:p> 

(Here I am showing report on Page Load, same code can be written on Button Click if wanted.)<o:p>

<o:p> 

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.<o:p>

Create Object of that as

private TableLogOnInfo LogInfo = new TableLogOnInfo();<o:p>

<o:p> 

The function can be written as<o:p>

<o:p> 

    private void SetLogonInfo()<o:p>

    {<o:p>

         try<o:p>

         {

LogInfo.ConnectionInfo.ServerName=”SerVerName”;

LogInfo.ConnectionInfo.UserID=”sa”;

LogInfo.ConnectionInfo.Password=”ok”

LogInfo.ConnectionInfo.DatabaseName=”CrystalSample”;<o:p>

        }<o:p>

       catch(Exception ex)<o:p>

        {<o:p>

         MessageBox.Show(ex.Message);<o:p>

        }        <o:p>

    }

<o:p> 

<o:p> 

<o:p> 

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

<o:p> 

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();<o:p>

    We can use this object to every report and need to give path while loading the report.as<o:p>

      O_Report.Load(@"E:\CrytstalSample\rptShowList.rpt");<o:p>

    <o:p>

b)    Create different objects for different reports as<o:p>

<o:p> 

     rptShowList O_showList = new rptShowList();<o:p>

<o:p> 

(Here I am using second option).<o:p>

<o:p> 

Make DataBase Connection as<o:p>

O_Report1.Database.Tables[0].ApplyLogOnInfo(LogInfo);<o:p>

<o:p> 

Step 6:- <o:p>

        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.<o:p>

<o:p> 

DataTable DT = new DataTable();<o:p>

       try<o:p>

           {<o:p>

              string CmdStr="";<o:p>

              CmdStr = “Select * from Vw_AddressList where User_Number>15”;<o:p>

               Connection.Open();              <o:p>

              SqlDataAdapter Adpt=new SqlDataAdapter(CmdStr,Connection);<o:p>

              DT.Clear();<o:p>

              Adpt.Fill(DT);              <o:p>

           }<o:p>

           catch(Exception ex)<o:p>

           {<o:p>

              MessageBox.Show(ex.Message);              <o:p>

           }<o:p>

           finally<o:p>

           {<o:p>

              Connection.Close();<o:p>

        }        <o:p>

    <o:p>

<o:p> 

Step 7:-<o:p>

    Now set the above filled DataTable as Reports dataSource.<o:p>

<o:p> 

    O_showlist.Database.Tables[0].SetDataSource(DT);<o:p>

<o:p> 

Step 8: - <o:p>

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

CryStalReportViewer1.ReportSource = O_Report1111;

CryStalReportViewer1.Zoom(1);<o:p>

<o:p> 

How to Change the properties of Fields on report programmatically.<o:p>

<o:p> 

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

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

<o:p> 

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

<o:p> 

FieldObject fUserNumber = FieldObject)o_showlist.Sections3.ReportObjects["Field1"];<o:p>

<o:p> 

fFullName1.ApplyFont(new Font(Arial,8.25F));<o:p>

<o:p> 

Same can be done with Text Object.<o:p>

<o:p> 

<o:p> 

We can change properties of Formula Field in different way also.<o:p>

O_showlist.DataDefinition.FormulaFields["Header"].Text = “Address List”;<o:p>

<o:p> 

<o:p> 

Formula Fields:- <o:p>

<o:p> 

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

<o:p> 

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

<o:p> 

·         Right Click on Formula Field -> New<o:p>

·         Give Formula Name.<o:p>

The Formula Editor screen will appear as follows-<o:p>

<o:p>

<o:p>Sample screenshot 

<o:p> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.<o:p>

<o:p> 

Now I have written code as –<o:p>

<o:p> 

if({Vw_AddressList.Gender})= 'M' then<o:p>

'Male'<o:p>

else<o:p>

'Female'<o:p>

<o:p> 

<o:p> 

<o:p> 

<o:p> 

<o:p> 

<o:p> 

Deploying the <st1:city w:st="on"><st1:place w:st="on">Crystal Report Dot Net Application:<o:p>

<o:p> 

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

<o:p>

<o:p>Sample screenshot 

<o:p> 

<o:p> 

<o:p> 

<o:p> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Select SetUp and Deployment Projects -> Setup Project.<o:p>

Give name to your Setup.<o:p>

<o:p> 

Step 2:- To add Exe and other files-<o:p>

        <o:p>

    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.<o:p>

    Up to this is general Deployment process. If your application includes Crystal Reports you need to do some extra.<o:p>

<o:p> 

a)    Adding Merge Module:-<o:p>

·         Right Click on Setup project -> Add -> Merge Module<o:p>

·         Select ‘Crystal_regwiz2003.msm’, Crystal_Managed2003.msm,           Crystal_Database_Access2003_enu.msm, Crystal_Managed2003.msm<o:p>

·         Click on Open.<o:p>

<o:p> 

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.<o:p>

    In properties select ‘(MergeModuleProperties)’ -><o:p>

 Enter your key at ‘License Key‘.<o:p>

Now the License key can be your Crystal Report product key or you can get it from,<o:p>

Help(Menu of dot net) -> About Microsoft Development Environment.<o:p>

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'.<o:p>

<o:p> 

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