Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
10.7%
Crystal Reports




by Eranda Niroshan



    Transforming row data into usable information is a quit complex and very creative task. Crystal reports provides simple and well designed environment to generate high standard reports. That’s why crystal reports heavily used in business world. This is the second article of the crystal reports. Let’s take brief idea about what we discussed in last session.

    “Crystal Reports is a advanced repost generating software. It’s a separate commercial software product, but this article is about the crystal Reports comes with Visual Studio.NET. You can simply add a report into your project using insert new item. Each report has 5 main parts, those parts is used for represent different kind of data. You can bind database table into your report using Database Expert wizard that located under field explorer”

Within this article we are going to learn few advanced topics and practical problem arise when developing software with crystal reports. 


How to Generate Crystal Reports Document with Manual Data.

   
Most of the time we need print local data with the report as an example “print the name of the Customer which selected in the drop down menu”. Crystal reports also has its own variables, developers cannot directly use them like local variables. Suppose there has a crystal report variable called “customeName” you cannot directly assign value of a text box to it (customeName = textBox1.text; ).  Now let’s how to insert parameter ( variable ) field into crystal report.

  •     First right click on the project name at the solution explorer and select Add  New Item and create a report document.
  •     After that open the report and right click on the “parameter field” under “field explorer” and select “New”, this will open Create Parameter Field dialog
  •     Give a name for the parameter , select t a value type and press ok.



  •     Now your new parameter field will place under “parameter field”. Click the + icon and expand the tree.
  •     Then drag and drop it into the any part of the repost you like. That’s the variable you are going to replace with custom value. Now you can put a label beside.
  •     Go to tool bar drag and drop text object and give a name for the object.
  •     Both of these elements are text objects so you can format them as you wish. To do that right clicks on the object and select format object.



Now parameter creation and declaration part finished. Then we should access the report object via programming code and assign values to it.

  •     Drag and drop a Crystal report viewer into the project and name it as” rpViewer”.
  •     Then double click the form and insert following code into program load method.

{

customerReport  cr = new customerReport();

ParameterDiscreteValue val = newParameterDiscreteValue();

 

val.Value = "Mr.Mahinda Rajapakse";

rv.ParameterFields["topic"].CurrentValues.Add(val);

rpViewer.ReportSource = cr;

}


  
Then save the program and run it. Name of the president will be show in the report.


Deploy a project which uses Crystal reports.


Usually database connection details of your program will keep in a xml file or text document. After you deploy the project you can change those details as its working environment. But when you deploy a project with crystal reports it’ll not working like that. The reason is crystal report capture database path and other details while you add database tables on its development time. There for we should write some codes to capture the database details from the working environment. Following example describe how to capture and assign DB details to the report.

Add these three references to the top of the document

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

using CrystalDecisions.Windows.Forms;



And update the loading method like below.


{

customerReport  cr = new customerReport();

 

ConnectionInfo connection = new ConnectionInfo();

 

connection.DatabaseName = /*data base name from xml or text file*/;

      connection.ServerName = /*server name from xml or text file*/;

      connection.UserID = /*userId from xml or text file*/;

connection.Password = /*password from xml or text file*/;

 

foreach (CrystalDecisions.CrystalReports.Engine.Table table in

    cr.Database.Tables)

      {

            // Cache the logon info block

            TableLogOnInfo logOnInfo = table.LogOnInfo;

 

           // Set the connection

            logOnInfo.ConnectionInfo = connection;

 

           // Apply the connection to the table!

           table.ApplyLogOnInfo(logOnInfo);

      }

 

 

ParameterDiscreteValue val = newParameterDiscreteValue();

 

val.Value = "Mr.Mahinda Rajapakse";

rv.ParameterFields["topic"].CurrentValues.Add(val);

rpViewer.ReportSource = cr;

}



First example does not need such kind of code because it doesn’t use any database. But this will be very helpful when you use database. Following image are examples for development time report and output  report which used above examples.


Share/Save
No votes yet

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options