f461fcd5 2f9a 4313 A938 Ed22ecb7a7e2Crystal Report BCS III

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Crystal Reports

Crystal Reports is a business intelligence application, currently marketed to small


businesses by SAP SE. It is used to design and generate reports from a wide range of
data sources.

Crystal Reports is a popular Windows-based report writer (report generation


program) that allows a programmer to create reports from a variety of data sources
with a minimum of written code. Developed by Seagate Software, Crystal Reports can
access data from most widely-used databases and can integrate data from multiple
databases within one report.

Steps to create Crystal Report


Open Visual Studio .NET and select a new Visual Basic .NET Project.

Create a new Crystal Reports for Product table from the above database crystalDB. The Product Table has
three fields (Product_id,Product_name,Product_price) and we are showing the whole table data in the
Crystal Reports.

From main menu in Visual Studio select PROJECT-->Add New Item . Then Add New Item dialogue will
appear and select Crystal Reports from the dialogue box.
Select Report type from Crystal Reports gallery.

Accept the default settings and click OK.

Next step is to select the appropriate connection to your database. Here we are going to select OLEDB
connection for SQL Server

Select OLE DB (ADO) from Create New Connection .


Select Microsoft OLE DB Provider for SQL Server .

Next screen is the SQL Server authentication screen . Select your Sql Server name , enter userid ,
password and select your Database Name . Click next , Then the screen shows OLE DB Property values ,
leave it as it is , and click finish.
Then you will get your Server name under OLEDB Connection from there select database name
(Crystaldb) and click the tables , then you can see all your tables from your database.

From the tables list select Product table to the right side list .

Click Next Button

Select all fields from Product table to the right side list .
Click Finish Button. Then you can see the Crystal Reports designer window . You can arrange the design
according your requirements. Your screen look like the following picture.

Now the designing part is over and the next step is to call the created Crystal Reports in VB.NET through
Crystal Reports Viewer control .

Select the default form (Form1.vb) you created in VB.NET and drag a button and CrystalReportViewer
control to your form.
Select Form's source code view and put the code on top

After you run the source code you will get the report like this.
Examining the Report Designer
Each report within your application is just like any other component that your application uses. It is
listed in the Solution Explorer window as a class in your project. When you double-click it, it opens the
report in design mode and you can make changes to it.
Each report starts with five sections: Report Header, Page Header, Detail, Page Footer, and ReportFooter.
These sections are described in Table 1-1.

To the left of the report layout is the Toolbox. When you have the report designer open, there are only a
few controls available in the Toolbox. They are the Text Object, Line Object, and Box Object. These are
the most basic of the controls available.

Formatting Objects
Formatting objects within your report can be done by using the Format Editor.

In order to open the Format Editor, you must right click on the object you wish to format and select,
depending on what type of object you have selected, Format Field, Format Text, or Format Object.
Whatever you have, the Format … option opens the Format Editor. The Format Editor dialog box presents
several different formatting options depending on the type of field you are attempting to format.
Adding Lines and Boxes to Your Report
Crystal allows you to precisely add a line or a box to your report. Add them by • Clicking the Insert Line
icon or the Insert Box icon on the Supplementary Toolbar.Note: Your mouse pointer changes to the shape
of a pencil. • Draw the line or box with the mouse pointer. Edit the line or box by right clicking on them
and choosing the Format Line or Format Box option from the menu.
Inserting Special Fields
Crystal Reports contains several special fields that are unrelated to fields within the
database.The special fields included are the following

Special Field Description


Page Number Automatically prints number on each page
Total Page Count Calculates and prints total report pages.
Page N of M Inserts page number with a page count (ex:Page 2 of 6).
Report Title Places the report title as indicated in File/Summary Info.
Report Comments Places the report comments as indicated in File/Summary Info.
File Path and Name Inserts the file path and name of the report
File Author Inserts the author as indicated in File/Summary info
File Creation Date Inserts the date the report was created
Print Date Takes the date from your PC’s calendar
Print Time Takes the time from your PC’s clock
Data Date Prints the date the datawas last refreshed
Data Time Prints the time the datawas last refreshed
Modification Date Shows the date thereport was last saved
Record Number Assigns a number to each record in the report and acts as a counter
field
Group Number Allows numbering of each group in the group header or footer
Record Selection Formula Inserts Record Selection formula as created in the Record
Selection Formula Editor
Group Selection Formula Inserts Group Selection Formula as created in the Group
Selection Formula Editor.
To add a special field, open the Field Explorer by:
• Clicking the Insert Fields icon on the Standard Toolbar.
• Click the plus icon beside the Special Fields to see the fields.
• Insert the special field in the same manner that you would a database field

Crystal Reports from Query Strings


We can create Crystal Reports in VB.NET using SQL Query String . Here we create a Strongly Typed
dataset for Crystal Reports design and create a connection object and execute the SQL Query String .

First create a strongly typed datset and its Crystal Reports design . After create the dataset and Crystal
Reports design you should create a connection object and fetch the data from database.

Select the default form (Form1.vb) you created in VB.NET and drag one button and CrystalReportViewer
control to your form.

Put the following source code in the button click events

Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

Dim ds As New DataSet1


Dim cnn As SqlConnection
Dim connectionString As String
Dim sql As String

connectionString = "data source=servername; _


initial catalog=crystaldb;user id=username;password=password;"
cnn = New SqlConnection(connectionString)
cnn.Open()
sql = "SELECT Product_id,Product_name,Product_price FROM Product"
Dim dscmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet1
dscmd.Fill(ds, "Product")
MsgBox(ds.Tables(1).Rows.Count)
cnn.Close()

Dim objRpt As New CrystalReport1


objRpt.SetDataSource(ds.Tables(1))
CrystalReportViewer1.ReportSource = objRpt
CrystalReportViewer1.Refresh()
End Sub
End Class
Formula Fields
The formula field in a Crystal report is a functionality provided to enhance computation of values
dynamically during the display/printing of records in a report of an application

If you have a Crystal Reports with Qty and Price , you need an additional field in your Crystal Reports for
the Total of QTY X PRICE . In this situation you have to use the Formula Field in Crystal Reports.

In this tutorial we are showing the all orders with qty and price and the total of each row , that means each
in each row we are showing the total of qty and price. Before starting this tutorial.

Create a new Crystal Reports with fields CustomerName , Order Date , Product Name and Product Price .
In that report selecting only four fields , here we need one more field Prodcut->Price .

After you create the Crystal Reports you screen is look like the following picture :

Next is to create the a Formula Field for showing the total of Qty and Price .

Right Click Formula Field in the Field Explorer and click New. Then you will get an Input Message Box ,
type Total in textbox and click Use Editor

Now you can see Formula Editor screen . Now you can enter which formula you want . Here we want the
result of Qty X Price . For that we select OrderDetails.Qty , the multiplication operator and Product.Price .
Double click each field for selection.
Now you can see Total Under the Formula Field . Drag the field in to the Crystal Reports where ever you
want .

Now the designing part is over . Select the default form (Form1.vb) you created in VB.NET and drag a
button and CrystalReportViewer control to your form.

When you run this program you will get the following screen.
Creating a Group or GroupName fields
Once you have determined that you would like to insert a group into your report, you must
either:
• Press the Insert Group icon located on the Supplementary toolbar or
• Choose the group command in the Insert menu.

The Insert Group dialog box appears. This box gives you the option to choose the field in
which you want to group as well as allowing you to define sorting and other options.
There are four choices used to determine the sort order of your group. They are:
1. In ascending order
2. In descending order
3. In original order
4. In specified order
In addition to the sort choices, there are two additional check boxes for the following
features:

Keep Group Together – Assures that page breaks will not separate groups. Use this option
ONLY if your groups are small. If you turn this feature on and you have a group that is
longer than one page, Crystal will move the entire group to the next page.

Repeat Group Header – If you have large groups, you may want the group header to appear
at the top of each new page to identify the group. This feature will do this automatically.
When a group is created Crystal adds two additional sections to the report.
1. A Group Header
2. A Group Footer
Once your group is created you can edit it by:
 Right clicking on the gray area of the Group Header #1 section and
 Choosing the Change Group option from the shortcut menu.
 Right clicking groupname field under the Group name fields option in field explorer

Summarizing Group Data


Once you have grouped and sorted the records within your report, you may wish to insert
summary data. Inserting summaries is a very helpful tool. Summary information displays
automatically in the Group Header of the report and can perform numerous calculations.
Summaries can be performed on any type of data.
On string data, summaries are limited to:
• Counting
• Selecting the frequency

On numeric data, summaries can calculate:


• Sums
• Averages
• Minimums
• Maximums
• Counts
• Statistical Analyses
Crystal only gives choices that are appropriate for the type of data you are summarizing.
To summarize the report (after the report has been grouped) you must do one of the
following:
• Click the Insert Summary icon located in the Standard toolbar.
• Right click on the field (within the Details region) and choose Insert Summary from the
submenu.
• Select the field you wish to summarize and then click on the Insert menu and choose
Summary.

Parameter fields
Parameters are used to take user inputs before the report is generated. User has to answer
the prompt before the report is generated and the report output depends on the response of
the user for the parameter value.
By using parameters with formulas and in the report, you can create a single report that
changes according to the requirement of different users.
Creating a Parameter Field

• Select Insert > Field Object from the menu bar.

• The Field Explorer dialog box appears.

• Right-click on Parameter fields and select New

 The Create Parameter Field dialog box appears:

 Enter a name for the parameter field.

 Enter the Prompting text, which is a text message that will indicate what you should
do or select (for example, "Enter the subheading for the report" or "Enter Start
Date").

 Select the value type. The options are (examples of each type are shown in
parenthesis):
o Boolean (True or False)
o Currency ($534.12)
o Date (03/15/2002)
o DateTime (03/15/2002 2:49:09PM)
o Number (12345)
o String (text)
o Time (2:49:09PM)
 In the Options frame, set the parameter to accept discrete values, range of values, or
both.
 Click OK to return to the previous screen.
 Click OK to save the parameter field.

Once you establish a parameter, you can use it in a formula, selection formula, or on
the report as a field.

You might also like