Free - Reporting XML data using Crystal Reports
Reporting on data sources such as: MS Access, SQL Server and Oracle, is common with Crystal Reports (CR). How about XML as 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.
Detail Section
Detail section of report will have the CD
Catalog list details. Now the question is from where we'll get the data for it?
Well, as you know in the beginning we added the DataSet to this project, we
will use this DataSet as source of data to our report.
Adding DataSet as source of data to 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, 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).
Figure: 9 – Adding DataSet as report data source
Once 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 Title from Field Explorer inside
details section.
As you drag and drop the fields inside
detail section, you'll notice that tile of the filed also added to report
designer. As 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 repot design layout
Although the report design looks complete;
however, we have not yet worked on hierarchical functionality of this report.
As you can see in Figure 1, all the employee names should form hierarchy based
on ManagerID association.
Step 5: C# interface code
All right, we got all we needed as report
design. Please make sure to have following C# code behind Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace XMLCRReport
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,
EventArgs e)
{
DataSet dsReport = new
dsCDCatalog();
// create temp dataset to read xml
information
DataSet dsTempReport = new
DataSet();
try
{
// using ReadXml method of
DataSet read XML data from books.xml file
dsTempReport.ReadXml(@"C:\articles\XmlCrystalReport\cd_catalog.xml");
// copy XML data from temp
dataset to our typed data set
dsReport.Tables[0].Merge(dsTempReport.Tables[0]);
//prepare report for preview
rptXMLData rptXMLReport = new
rptXMLData();
rptXMLReport.SetDataSource(dsReport.Tables[0]);
crystalReportViewer1.DisplayGroupTree = false;
crystalReportViewer1.ReportSource = rptXMLReport;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
I started to code in 1990 using C and GW basic, then moved to 4GL world with DBIII, Clipper and FoxPro etc. and continued till I found and hooked to MS goody bag Visual Basic. For last four years, I am working extensively on .NET technologies and scripting both VB.NET and C#. My ideal choice of database is SQL Server; however, I do interact with Oracle when required. I have worked on all sort of Business applications, but my ideal project would be anything which has some Financial part associated with it, I wish I can put a Balance-sheet report in all the project I do… I also teach .NET related technologies as part-time faculty with local community college and try to help share my knowledge with others.
See All Postings From Asif Sayed >>
Reviews
Balance sheet
hi
can u pls. guide me how to print a balance sheet in crystal reports ? i mean T Format where the data from 1 table i.e. Libilities would be coming on 1 side while the Assets table would be coming on the other side. i am really confused as to how i can do this. i would love to have an solution to this.
thankx in advance.
kuldev
You must me logged in to write a review.