Crystal Reports And Its Use In C#

Introduction

Crystal reports allow the user to generate reports by summarizing the data. It also allows the user to present data in a graphical format. The crystal reports enterprise reporting consists of,

  • Powerful Report Design - Visual report designer allows user to design interactive reports, they can develop reports by using the .NET tools.
  • Application Development Flexibility - Crystal Reports allow the user to generate HTML pages directly without wasting time on coding the pages. This facilitates the user in focusing on other application logic. Crystal Reports simplifies the process by separating the application development and report design task.
  • Management and Delivery of Reports - Crystal Reports can be easily published on the web. The user can export reports to different file formats like PDF or DOC. The other features of Crystal Reports are printing and exporting report using minimal efforts, and high-quality reports.

Features

Crystal Reports incorporate various new features for easy report generation. Some of the features are,

  • Customizable Templates
    The user can design customized templates and use them while generating new reports. These templates can specify the formatting and design logic and the different data access operations to maintain consistency across multiple reports. This prevents the user from spending more time formatting every single report.
  • Powerful Formula Language
    Crystal reports provide over 160 built-in and user-defined functions and operators. This allows the user to control report formatting and complex logic. It also includes a call-stack to easily debug the generated errors.
    Asymmetric Reporting
    Crystal reports allow the user to display only relevant and required data by hiding unnecessary data.
  • Alerting
    Crystal reports allow the user to highlight certain information to the user by generating messages.
  • Hyperlinks
    Crystal reports allow the user to create interactive reports by using hyperlinks. The reports are provided with hyperlinks that link to various web documents.
  • Data Access
    Crystal reports allow the user to easily access databases, files, application systems, and so on. Crystal reports have more than 35 data drivers that help in easy access of data from various data sources. It provides flexibility in connecting to the database by using query generation feature of Crystal Reports.
  • Data Designing and Formatting
    Crystal reports provide various options for designing and formatting data. It provides various wizards to simplify the process of connecting to the database, sorting, or selecting data. It allows the user to provide graphical impact to the reports by using various maps and charts such as bar, pie, and so on.

Crystal Report Creation Wizard

Crystal Reports provides wizards for easy generation of reports. The report Wizard is used for creating and adding new reports to the existing report server. The steps for creating a report using the Report Wizard feature are as follows:

Step 1

From the file menu, click New and click Project

Step 2

In the Project Types list, select Business Intelligence Projects.

Step 3

In the Template list, select Report Server Project Wizard. The Welcome to the Report Wizard page appears.

Step 4

Click the Next Button.

Step 5

In Select the Data Source Page, click New Data Source and enter a data source name, select data type and the connection string. After building the connection click Next.

Step 6

In Design the Query page, type the query string and click Next.

Step 7

In the select the Report Type page, select Tabular or Matrix to arrange the reports in table format or matrix format respectively. Click Next.

Step 8

Depending on the choice made in the Select the Report Type page, the Design the Table page or Design the Matrix page is displayed. If the Design the Table page is displayed, click the appropriate field and click the Page, Group or Details button. If the Design the Matrix page is displayed, click the appropriate field and click the Page, Columns, Rows or Details button.

Step 9

From Choose the Tab Style or choose the Matrix Style page, select the appropriate option to apply styles to the reports, and click Next.

Step  10

In the Choose the Deployment Location Page, enter the report server and folder to publish the report

Step 11

In the Completing the Report Wizard, enter the name of the report, Click finish.

Customizing Reports

The Process of designing report in Crystal Reports can be optimized in many ways. Information can be summarized so that the user can navigate through the report quickly and also access the information within the reports. The benefits of creating user-driven reports are,

  • Provide interactive control over the data existing in the report.
  • Allows creation of reports as per user requirement to make the reports more useful.

On-Demand Subreports

In crystal reports, if a report has large number of records, they can be put into on-demand subreports. The on-demand subreports appear as hyperlinks in the primary report. When the user opens the primary report, the data from on-demand subreports are retrieved only when the user clicks the appropriate hyperlink.

The on-demand subreports are widely used when a user wants to add some additional data, which might not be useful for all other users. Thus, such additional data can be put into on-demand subreports so that only interested users click the hyperlink and view these reports.

Data Presentation

Crystal Reports allow the user to customize reports at runtime by accessing them. The user can customize the report in various ways. One of the methods is to group fields of a report at the runtime. The group field can be used to group data in the report as per the specified condition.

Consider a scenario where a report is generated in an application containing two fields (Employee Name), (Employee Age). A group is created in the report on the (Employee Name) field. The groups in the report can be changed by changing the selection in a combo box.

Source code demonstrates how the groups in the report can be changed.

ReportDocument rdEmployeesReport;
SqlDataAdapeter sqldaEmployees = new SqlDataAdapter(“SELECT * FROM Employees”, sqlconEmployees);
rdEmployeedReport = new ReportDocument();
rdEmployeedReport.Load(@\\ Windows Forms\ EmployeesReport.rpt”);
DataSet dsetEmployees = new DataSet();
sqldaEmployees.fill(destEmployees, ”Employees”);
foreach(DataColumn dco1 in destEmployees.Tables[“Employees”].Columns) {
    cboGroupField.Items.Add(dco1.ColumnName) l
}
cboGroupField.SelectedIndex = 0;
rdEmployeesReport.SetDataSource(dsetEmployees);
crvEmployeeList.ReportSource = rdEmployeeReport;
private void cboGroupField_SelectedIndexChanged(object sender, EventArgs e) {
    FieldDefinition FieldDef;
    FieldDef = rdEmployeesReport.Database.Tables[0].Fields[cboGroupField.Text];
    rdEmployeesReport.DataDefinition.Groups[0].ConnectionField = FieldDef;
    crvEmployeeList.RefreshReport();
}

In this code. An instance of SqlDataAdapter class is created and it retrieves the data from the Employees table. The ReportDocument class represents a report and allows the user to work with the report. The Load() method loads the EmployeeReport.rpt report. A dataset is created & the fill() method fills the dataset with the records. The column names are added to the cboGroupField combo box by using the Add() method of Items property. The SetDataSource() method binds the dataset to the report. A SelectedIndexChanged event is generated that allows the user to select the column name on which the fields should be grouped.

ReportViewer Controls

Report viewer controls are used for processing and displaying records that may contains data in tabular , aggregated or multidimensional form. Windows Forms provides the CrystalReportViewer control that allows the user to view and customize the reports. By using the properties, methods and events of CrystalReportViewer class, the appearance and functions of the reports can be customized.

Properties of CrystalReportViewer class

  1. DisplayStatusBar
    Specifies or retrieves a value that indicates whether the status bar is visible.
  2. DisplayToolBar
    Specifies or retrieves a value that indicates whether the toolbar is visible.
  3. ShowCloseButtton
    Specifies or retrieves a value that indicates whether a button for closing a report page exists in CrystalReportViewer control’s toolbar.
  4. ShowPageNavigateButtons
    Specifies or retrieves a value that indicates whether a button for navigating in a page exists in CrystalReportViewer control’s toolbar.
  5. ShowPrintButton
    Specifies or retrieves a value that indicates whether the print button on the toolbar is visible.
  6. ShowTextSearchButton
    Specifies or retrieves whether the search button on the toolbar is visible.
  7. ShowZoomButton
    Specifies or retrieves whether the zoom button on the toolbar is visible.
  8. ViewTimeSelectionFormula
    Specifies or retrieves the selection formulae that are used to filter data in the report.

Methods of CrystalReportViewer class

  1. CloseView
    Closes the view tab present in the control.
  2. ShowGroupTree
    Displays the group tree present in the control.
  3. ShowNextPage
    Displays the next page of the report.
  4. ShowPreviousPage
  5. Displays the previous page of the report.

Events of CrystalReportViewer class

  1. Drill
    Occurs when user goes deeper down in a report.
  2. HandleException
    Occurs when an exception occurs in the CrystalReportViewer control.
  3. ViewZoom
    Occurs when the zoom level of the control changes.

Source code demonstrates how to view the specified report using the CrystalReportViewer class.

SqlDataAdapter sqldaSuppliers=new SqlDataAdapter(“SELECT * FROM Suppliers”, sqlcon Suppliers);
ReportDocument rdSuppliersReport=new ReportDocument();
rdSuppliersReport.Load(@”\\Windows Forms\SupplierReport.rpt”);
DataSet dsetSuppliers=new DataSet();
sqldaSuppliers.Fill(dsetSuppliers,”Suppliers”);
rdSuppliersReport.SetDataSource(dsetSuppliers);
CrystalReportViewer crvSupplierList=new CrystalReportViewer();
crvSupplierList.Dock=DockStyle.Fill;
crvSupplierList.ShowExportButton=false;
crvSupplierList.ShowTextSearchButton=false;
crvSupplierList.ShowCloseButton=false;
crvSupplierList.ReportSource=rdSuppliersReport;
Controls.Add(crvSupplierList);

This code shows an instance of SqlDataAdapter class is created and it retrieves the data from the Suppliers table. The ReportDocument class represents a report and allows the user to work with the report. The Load() method loads the SuppliersReport.rpt report. A dataset is created & Fill() method fills the dataset with the records. The SetDataSource() method binds the dataset to the report. An object of CrystalReportViewer class is created namely crvSupplierList. The Dock property resizes the report to fit within the edges of control. Setting the ShowExportButton, ShowTextSearchButton, and ShowCloseButton Properties to false prevents the respective buttons to be displayed on the toolbar of CrystalReportViewer. The ReportSource property of the CrystalReportViewer object is set to the object of ReportDocument class. The Add() method adds the controls to the form.

Exporting a Report

Crystal Reports allows the generated report to be exported to various locations in various file formats. There are many ways in which the generated report can be exported. The most common way is to use the Export button on the CrystalReportViewer control. The user can also create a customized Export button by using the ExportReport() method of CrystalReportViewer class. This feature is only available in Windows Forms Viewer. The Steps for creating a customized Export button are as follows:

  • Insert a button into the form.
  • Double click the button and insert the following code on the click event of the button.

The following code demonstrates how to export a report by firing an event on button click.

private void btnExport_Click(object sender, EventArgs e) {
    crvSupplierList.ExportReport();
}

This code shows a button created with the name btnExport. When the user clicks the button, the ExportReport() method is invoked to export the report.

Summary

Crystal reports is a powerful report generation utility that provides various features for generating reports. Crystal reports introduce some powerful features like easy debugging, providing hyperlinks in reports, message alerts, and accessing various databases. Crystal reports creation wizard allows easy generation of reports by providing step-by-step guidance. The on-demand reports appear as hyperlinks in the primary report. This link allows the user to access the reports by clicking the appropriate hyperlink. Crystal Reports help in grouping the data fields in a report by specifying the different conditions.

The CrystalReportViewer class allows the user to customize the appearances and functionality of CrystalReportViewer control. The reports generated in Crystal Reports can be exported using the Export button or by using the ExportReport() method of CrystalReportViewer class.