Click here to Skip to main content
Click here to Skip to main content

Binding DataSet and Generic *.rdlc Reports to a ReportViewer at Runtime

By , 25 Jul 2006
Rate this:
Please Sign up or sign in to vote.

Introduction

In order to display a report by using ReportViewer Control, we need a DataSource that contains the data to be shown and a Report document that describes how that data should be displayed.

This article presents how to form a Report Display Component to display data within different DataSet objects that are described by a Report document (*.rdlc).

During my previous searches about displaying data within generic DataSet objects, I've encountered a set of different solutions. Most of them assumed DataSet objects are accessible during design time. Some others used TableAdapter objects that include SQL statements and/or Connection strings, etc. Probably, these solutions cover most of the situations a developer can encounter. But what if you don't have access to DataSet objects during design time, what if the overall architecture of the software doesn't let you to use SQL statements or connection strings on the architectural level you are currently working, what if you need to use data without knowing its structure until runtime, what if you need dynamic data sources rather than static?

I'm currently in need of a component that displays generic reports. My organization is developing accounting software and in accounting domain there are lots of changes in reports. After distributing and deploying the software, organization must give supporting service about newly added or changed reports. In the maintenance phase, it is costly to distribute a whole update that often. So what we need is some kind of a Report Import / Generic Report Display ability.

Before We Begin

As I've said before, in order to display a report, we need a DataSource (in this article, a populated DataSet instance) and a report document (*.rdlc file ). Once we have the Report-DataSet pair, we can display any report we want.

Logical representation of the Report Display Component

Figure 1a: Logical representation of the Report Display Component

How Do We Use DataSet as a Data Source?

A DataSet is an object that includes tabular data which can be represented by using XML format. What we need to know is that the structure of the data can be described by an XSD schema document which is also another XML based document.

DataSet <code>dsHesapPlan = new DataSet();

//...
//... Some code to populate data within the DataSet dsHesapPlan
//...

// Set the name of the DataSet which will be useful later
dsHesapPlan.DataSetName = "HESAP_PLAN";

// Can be used to write the of the DataSet
dsHesapPlan.WriteXmlSchema();

// Can be used to write the data within the DataSet as XML
dsHesapPlan.WriteXml();

Figure 1.b: Useful operations on a DataSet

Let's take a look at the schema of the DataSet, which we get by running the method dsHesapPlan.WriteXmlSchema():

Sample DataSet Schema which will be useful when designing the report.

Figure 1.c: Sample DataSet Schema which will be useful when designing the report.
Highlighted areas show naming information that will be used later.

What is a *.rdlc Report Document?

An rdlc Report Document is also another XML based document that defines how the data should be displayed. I'm not going to demonstrate how to form a report here. But there are some things you should know, so I'll form a checklist of actions to build a report.

  1. Form a schema of the DataSet which you will use (dsHesapPlan.WriteXmlSchema()).
  2. Add the schema to your Visual Studio Project. The schema will be visible within the Data Sources window.
  3. By using this schema, design your report.
  4. After you complete designing the report, save the project.
  5. Inside your project folder, you'll find the *.rdlc report document. Take it for redistribution.

Here is a part of the structure of a *.rdlc Report Document:

Sample .rdlc Report Document

Figure 1.d: Sample *.rdlc Report Document.
Highlighted areas show naming information which must show consistency with your DataSet definitions.

How to Bind a DataSet-Report Pair?

So, now we have a Report and a DataSet that can be bundled and sent to the client. Now what we need is a system to know what to do with this DataSet-Report pair.

Note that, you can send the report as the *.rdlc file itself. And DataSet can be distributed in a set of different ways. For example, executing CREATE scripts in client side to generate STORED PROCEDUREs that return the data forming the desired DataSet, or, by XML data itself, etc.

This part of the article assumes you have the properly designed Report document and the DataSet.

The following code shows how to bind a Report-DataSet pair to a ReportViewer object dynamically.

// A method to populate data inside the referenced DataSet
getDataSet(ref <code>dsHesapPlan);

// Set the path of the rdlc document
string reportPath = "Reports/AccountReport.rdlc";

// Initiate ReportViewer object
ReportViewer rView = new ReportViewer();
rView.Dock = DockStyle.Fill;
this.Controls.Add(rView);

// Add data source to the local report
// Note that the name should either be passed as parameter or 
// parsed from the report document
rView.LocalReport.DataSources
       .Add(new ReportDataSource("HESAP_PLAN_dtLast", dsHesapPlan.Tables[0]));

// Set the active report path of the ReportViewer object
rView.LocalReport.ReportPath = reportPath;

Figure 1.e: How to bind the DataSet and the generic Report dynamically.

Conclusion

This technique may be useful for developers experiencing similar problems with me. Surely, it is possible to build a real smart system to understand and display a report. I hope this article guides efforts for building such a system.

You may visit www.rdleditor.com for a series of related links.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DIren
Web Developer
Turkey Turkey
No Biography provided

Comments and Discussions

 
Questiongreat to see this coding community Pinmembersteptome26-Mar-13 7:25 
QuestionArticle PinmemberSherry3324-Mar-13 9:21 
QuestionVery useful PinmemberIlie Florian12-Mar-13 11:37 
Questionconnect dataset to RDLC Report as DataSource PinmemberCool Smith7-Mar-13 17:21 
QuestionIts very interesting Pinmembermrdonkey216-Feb-13 12:14 
Generalfantastic job PinmemberPaulo Neto9-Feb-13 9:21 
Questionvery interesting Pinmembertotyou131-Feb-13 5:09 
QuestionHave a question Pinmemberslime1315-Dec-12 5:56 
Generalreporting software Pinmemberlalalbut30-Aug-12 18:43 
GeneralMy vote of 5 Pinmemberbhalodiyapragnesh5-May-11 7:03 
I found my Solution Regarding Dynamic Dataset Binding in Report Through This....Excellent..>!!
QuestionI want to display in reportViexer1 report.rdlc data from a xml file with VB.net if possible Pinmemberelmeksoaui110-Mar-11 12:49 
NewsHave you checked out the RDLReportViewer control... Pinmemberchris17516-Apr-10 18:18 
QuestionRemote Report PinmemberAcidAndroid17-Dec-08 12:04 
AnswerRe: Remote Report PinmemberDIren17-Dec-08 20:50 
GeneralRe: Remote Report Pinmemberdg782-Sep-09 4:42 
GeneralRe: Remote Report PinmemberDIren2-Sep-09 5:36 
QuestionReport is ok but subreport PinmemberAcidAndroid12-Dec-08 8:57 
AnswerRe: Report is ok but subreport PinmemberDIren12-Dec-08 9:17 
GeneralRe: Report is ok but subreport PinmemberAcidAndroid17-Dec-08 13:32 
GeneralSome problems Pinmember_str_618-May-08 5:44 
GeneralRe: Some problems PinmemberDIren14-May-08 22:30 
GeneralRe: Some problems PinmemberHari Om Prakash Sharma6-Jul-08 20:30 
GeneralRe: Some problems PinmemberDIren7-Jul-08 0:55 
GeneralRe: Some problems PinmemberHari Om Prakash Sharma7-Jul-08 2:18 
GeneralRe: Some problems Pinmembersanzopaol9-Feb-11 7:51 
GeneralRe-loading the report data to the ReportViewer PinmemberAlec MacLean7-Mar-08 6:10 
QuestionHow to fill dataset derived from XSD file? PinmemberColizobble9-Oct-07 1:29 
AnswerRe: How to fill dataset derived from XSD file? PinmemberDIren9-Oct-07 3:07 
GeneralRe: How to fill dataset derived from XSD file? PinmemberColizobble9-Oct-07 3:34 
Question3d charts in rdlc files Pinmemberkriskan4-Sep-07 18:48 
AnswerRe: 3d charts in rdlc files PinmemberDIren5-Sep-07 1:45 
GeneralTransform DataSet Xml Schema to RDLC Pinmembernbohr99a16-Aug-07 15:33 
QuestionHow to bind data source in rdlc Pinmemberimran_Shaikh4-Jul-07 6:24 
AnswerRe: How to bind data source in rdlc PinmemberDIren5-Jul-07 2:00 
QuestionNot Working................... [modified] Pinmemberallanace85226-Jun-07 1:23 
Questiondataset XML from pivoted table Pinmemberajbarton197221-Jun-07 7:03 
AnswerRe: dataset XML from pivoted table PinmemberDIren22-Jun-07 3:32 
QuestionRDLC Generator PinmemberSu_shamim23-May-07 20:42 
AnswerRe: RDLC Generator PinmemberDIren17-Jun-07 21:12 
GeneralSaving report to the file [modified] Pinmembermishasoft12-May-07 9:38 
GeneralRe: Saving report to the file PinmemberDIren17-Jun-07 20:50 
GeneralNewbie in Reportviewer Pinmembercocoonwls19-Apr-07 20:24 
GeneralRe: Newbie in Reportviewer PinmemberDIren22-Apr-07 23:40 
GeneralRe: Newbie in Reportviewer Pinmembercocoonwls25-Apr-07 6:04 
GeneralBest Article Ever! Pinmemberharindaka17-Apr-07 23:41 
GeneralRe: Best Article Ever! PinmemberDIren18-Apr-07 1:40 
GeneralReport Pinmembermadhuri.hyd@cnkonline.com9-Apr-07 23:38 
GeneralRe: Report PinmemberDIren18-Apr-07 1:38 
GeneralTake a long time for report Loading PinmemberArek Suroboyo21-Mar-07 19:38 
GeneralRe: Take a long time for report Loading PinmemberDIren22-Mar-07 3:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 25 Jul 2006
Article Copyright 2006 by DIren
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid