Total votes: 1
Print: Print Article
Please login to rate or to leave a comment.
Published: 28 Jun 2007
Download Sample Code
This article will show you how to report on XML data with Crystal Reports and Windows Forms client.
Figure 1: Example in run-time mode
Reporting on data sources such as: MS Access, SQL Server and Oracle, is common with Crystal Reports (CR). How
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
What is XML Data?
Before we see the demo, let’s get to know what
XML data is! Put in simple words,
stands for Extensible Markup Language. It is an initiative by W3C to allow the exchange of information.
XML is easy to understand by both humans and computers. The information stored in
format can work on cross platform scenarios. For example, if you have information in
XML format, it
is easy to share them between the UNIX and the Windows platform. The data inside an
XML file is
presented with user defined tags. Following is the partial listing from the sample
XML file which I
am using in the demo:
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.
Step 1: Let’s create a Windows Application Project
If VS 2005 IDE is not already started, then you can launch the IDE by clicking on windows Start button ->
All programs -> Microsoft Visual Studio 2005 -> click on icon Microsoft Visual Studio 2005. You may have
others ways to lunch the IDE, such as clicking it on your desktop and so on.
- Click on menu File -> New-> Project… or you can press the hot key Ctrl + Shift + N (please see
- From New Project dialog box select -> Visual C# -> Windows
- From Templates
select -> Windows Application
- 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.
- Click on OK button to finish the process.
VS 2005 will create a new project and add
Form1 to it as default.
Figure 2: Process to create New Windows Application Project
Note: You can always access the menu View -> Toolbox or press Ctrl + Alt + X, to make Toolbox
window visible. To get the maximum space on designer surface, you may like to use Auto Hide feature of
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.
Table 1: Properties of Form1
|Text||Reporting XML data using Crystal Reports
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.
You can add a CRV to the
Form1 as follows:
- Make sure
Form1 is currently open in designer.
- Drag ToolBox -> CrystalReports
-> CrystalReportViewer and drop it on
Form1. This step will create a new instance of CRV with the
name crystalReportViewer1. You can name it as you prefer; I’ll stick with the default.
After the step 1 and step 2, your project should look similar to Figure 3.
Figure 3: Form1 designer surface after adding CRV
Step 3: Add a DataSet to the project
Although there are different ways you can get the data to CRV, I’ll show you here the
technique. So, let’s go ahead and add a
DataSet to the project. The following step is needed to have
DataSet added to the project:
Select Add -> New Item ->
DataSet from Solution Explorer. Change the name from
dsCDCatalog and click the Add button. Please Cancel the TableAdapter
Configuration wizard; we’ll add a
DataTable using the
Let’s add the
DataTable to our newly created
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
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).
Figure 4: Add DataTable to DataSet
Let’s start adding columns to
dtCDCatalog). Your designer screen should look like Figure 5.
dtEmployee and select Add ->
Column to start adding the columns to
Figure 5: Add columns to DataTable
Repeat the action for the
- Title (String)
- Artist (String)
- Country (String)
- Price (Double)
- Year (String)
As you are adding the columns, the default data type is
string. Go to the Properties windows
after selecting a column to change it from
Int32. After you are done with
adding columns, it should look similar to Figure 6.
Figure 6: DataTable after adding needed columns
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 (
Select Add -> New Item -> Crystal Report from Solution Explorer. Change the name from
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
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.
Figure 7: Newly added report in design mode
Adding a report to
the project is easy. The challenge is to fill in the empty report layout. Whether this is your first report or
you are a reporting junkie like me; we must deal with the most basic building blocks of the report writing. That
is, the header, the detail, and the footer.
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.
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.
Figure 8: Report Header and Footer
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.
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
DataSet as the source of data for our report.
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
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).
Figure 9: Adding DataSet as report data source
Once the report
source is specified, all we have to do is drag and drop the fields from
DataTable inside Detail
section on report designer surface. You can drag and drop all the fields from
with “Title” from Field Explorer inside the details section.
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.
Figure 10: Final report design layout
That’s it. This is what we
need for the design part. Now let’s write some cool
C# code to bring our report to life.
Step 5: C# interface code
All right; we got all we needed as report design. Please make sure to have following
Let’s build and run the example
Let’s build the project. You can build a project in several different ways. If you just have one project in
solution; building solution and project are the same. You can click on the small green Play button on the main
toolbox, or hit
F5 on the keyboard to start the application. If all goes well then you report output
should look similar to Figure 1. If you face any issue, then I’d advice you to validate all the steps and try
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 firstname.lastname@example.org.
Sorry, no bio is available
View complete profile here.
You might also be interested in the following related blog posts
An alternative to Crystal
Stimulsoft Reports. New versions of reporting tools for .NET, Web, and WPF
A Unique Silverlight Viewer for Reporting Services Aimed to Display Reports from Reporting Services in Silverlight Applications.
Report writer for WPF. Long-expected tool.
A Bad Idea, EF Entities over WCF
Better XAML By Farr: WPF Line of Business Seminar
WPF / Silverlight: RadChart and BindableLinq
CodeDigest.Com Article,Codes,FAQs - April,2009
"The security validation for this page is invalid" when calling the SharePoint Web Services
XmlSchemaSet Thread Safety
Please login to rate or to leave a comment.