Crystal Reports Binding XML Data
Crystal Reports Binding XML Data
Crystal Reports Binding XML Data
This article will show you how to report on XML data with Crystal Reports and
Windows Forms client.
Introduction
Reporting on data sources such as: MS Access, SQL Server and Oracle, is
common with Crystal Reports (CR). How about XML as the data source? Well,
sure, you might be wondering how to do that? Is it even possible? Well, with help
of this article I’ll show you how to use the XML data source and report on it using
CR.
I assume the reader has basic understanding of the Visual Studio (VS) 2005 IDE
and Crystal Reports. You should also be comfortable writing code in C#. Basic
understanding of XML schema and ADO.NET is desirable.
1. <CD>
2. <TITLE>Empire Burlesque</TITLE>
3. <ARTIST>Bob Dylan</ARTIST>
4. <COUNTRY>USA</COUNTRY>
5. <COMPANY>Columbia</COMPANY>
6. <PRICE>10.90</PRICE>
7. <YEAR>1985</YEAR>
8. </CD>
As you can see, each <CD> tag is equivalent to a row with information related to a
CD. Furthermore, each column is uniquely identified as a tag. You can learn more on
XML here. All right, let’s start with our demo and see how easy it is to report on XML
data with CR and Windows Forms.
1. Click on menu File -> New-> Project… or you can press the hot key Ctrl + Shift +
N (please see Figure 2)
2. From New Project dialog box select -> Visual C# -> Windows
3. From Templates select -> Windows Application
4. Please give a name to the application; I’ve called the project as “XMLCRReport”.
You may choose a different location for storing the application files as per your
preference. Please see Figure 2 for illustration of the naming process.
5. Click on OK button to finish the process. VS 2005 will create a new project and
add Form1 to it as default.
Let’s set the properties of the Form1 according to Table 1. In case the property
toolbox is not visible in IDE, you may hit the F4 key to make it visible. Pease make
sure to select Form1 before applying changes to properties using the property
toolbox.
Property Value
Text Reporting XML data using Crystal Reports
700,300
Step 2: Let’s add CrystalReportViewer to Form1
Crystal Report Viewer (CRV) control is needed for us to show the report to the user.
Report Viewer gives life to your reports. Not only it lets you preview the output; it
also allows you to generate the information in various popular formats (PDF, Excel,
etc.). You can also take a hard copy print of the report while you are viewing the
output.
After the step 1 and step 2, your project should look similar to Figure 3.
Select Add -> New Item -> DataSet from Solution Explorer. Change the name from
DataSet1 to dsCDCatalog and click the Add button. Please Cancel the
TableAdapter Configuration wizard; we’ll add a DataTable using the DataSet
Designer.
Let’s add the DataTable to our newly created DataSet. A DataTable is essential
to load the reporting data; we’ll use the information from the DataSet/DataTable while
designing the report. The following step is required to have DataTable added to
DataSet (dsCDCatalog):
Double-click on dsCDCatalog from Solution Explorer; this will open the designer
view. Right-click the designer surface and select Add -> DataTable. Click the header
and change the name to dtCDCatalog (see the Figure 4).
• Title (String)
• Artist (String)
• Country (String)
• Company (String)
• Price (Double)
• Year (String)
So far we’ve created the project, and added a CRV and a DataSet. It’s time to
work on the report. Following are the steps required to add Report
(rptXMLData.rpt):
Select Add -> New Item -> Crystal Report from Solution Explorer. Change the name
from CrystalReport1.rpt to rptXMLData.rpt and click the Add button to
complete the action. A dialog box of Crystal Reports Gallery will appear. Select “As a
Blank Report” option and click the OK button to complete the process to add report.
By default, you will see that rptXMLData.rpt is open for you in designer mode
and its default setting. For this example I’ll make use of only Page Header and
Footer. I’ll reduce the height of Report Header and Footer. Typically, after all this
your report designer should look similar to Figure 7.
Typically, reports are designed with specific page size and layout in mind. Our report
is Letter size and Portrait layout. You can explore various properties attached to the
report layout by right-clicking anywhere on the open designer surface (gray area)
and selecting Report and Design Properties.
It is always advisable to draw a prototype of your report on paper before you start
the design process. As you can see in Figure 1, we have the report name and
report date in the header section. The body section has the employee list
information; the footer carries the page numbers.
Header Section
Our header section is consists of five Text Object, two Line Object and one
Special Field. Please drag and drop Text Object and Line Object from Toolbox ->
Crystal Reports. You can drag and drop the “Print Date” special field from Field
Explorer -> Special Fields -> Print Date. You’ll also notice that I’ve changed the
color and font after placing the report object on the designer surface. Please make
sure your header looks similar to Figure 8.
Footer section is simple; just one special field and one LineObject. As you’ve
done with the header section, you can drag and drop a LineObject from
Toolbox -> Crystal Reports. Similarly, you can drag and drop “Page N of M” special
field from Field Explorer -> Special Fields -> Page N of M. Please see Figure 8 and
make sure the Footer look similar.
Detail Section
The Details section of the report will have the CD Catalog list details. Now the
question is: From where will we get the data? Well, as you know, at the beginning
we added a DataSet to this project. We will use this DataSet as the source of
data for our report.
Adding a DataSet as source of data for a report is easy. You’ll start with Right-
Clicking on any open area on Report Designer -> Database -> Database Expert…
As a result of this action, the Database Expert dialog box will appear. You can click
on Project Data and expand the node ADO.NET DataSet. Please select the
dtCDCatalog DataTable from our DataSet and click on “>” button to
move it to selected tables section on right-hand side (Please see Figure 9).
As you drag and drop the fields inside the detail section, you’ll notice that the
title of the field is also added to report designer. Since we’ve already taken care
of field header, just delete the default added field header. Please make sure the
final report designer layout looks similar to Figure 10.
1. using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Drawing;
6. using System.Text;
7. using System.Windows.Forms;
8.
9. namespace XMLCRReport
10. {
11. public partial class Form1 : Form
12. {
13. public Form1()
14. {
15. InitializeComponent();
16. }
17.
18. private void Form1_Load(object sender, EventArgs e)
19. {
20. DataSet dsReport = new dsCDCatalog();
21.
22. // create temp dataset to read xml information
23. DataSet dsTempReport = new DataSet();
24.
25. try
26. {
27. // using ReadXml method of DataSet read XML data from books.xml f
ile
28. dsTempReport.ReadXml(@"C:\articles\XmlCrystalReport\cd_catalog.xml"
);
29.
30. // copy XML data from temp dataset to our typed data set
31. dsReport.Tables[0].Merge(dsTempReport.Tables[0]);
32.
33. //prepare report for preview
34. rptXMLData rptXMLReport = new rptXMLData();
35. rptXMLReport.SetDataSource(dsReport.Tables[0]);
36. crystalReportViewer1.DisplayGroupTree = false;
37. crystalReportViewer1.ReportSource = rptXMLReport;
38. }
39. catch (Exception ex)
40. {
41. MessageBox.Show(ex.Message);
42. }
43. }
44. }
45. }
Conclusion
As you can see, presenting XML data using Crystal Reports is easy and
straightforward. A small tweaking can bring value to the quality of data
presentation. Thank you for reading. As always, I’m looking for comments and
suggestion. Feel free to drop them at [email protected].