Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
4.42/5 (27 votes)
25 Jul 2006CPOL4 min read 584.6K   115   91
Binding DataSet and Generic *.rdlc Reports to a ReportViewer at runtime

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.

C#
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.

C#
// 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)


Written By
Web Developer
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHello , Pin
Azzam Alrandi28-Sep-14 9:47
Azzam Alrandi28-Sep-14 9:47 
Questiongreat to see this coding community Pin
steptome26-Mar-13 7:25
steptome26-Mar-13 7:25 
QuestionArticle Pin
Sherry3324-Mar-13 9:21
Sherry3324-Mar-13 9:21 
QuestionVery useful Pin
Ilie Florian12-Mar-13 11:37
Ilie Florian12-Mar-13 11:37 
Questionconnect dataset to RDLC Report as DataSource Pin
Cool Smith7-Mar-13 17:21
Cool Smith7-Mar-13 17:21 
QuestionIts very interesting Pin
mrdonkey216-Feb-13 12:14
mrdonkey216-Feb-13 12:14 
Generalfantastic job Pin
Paulo Neto9-Feb-13 9:21
Paulo Neto9-Feb-13 9:21 
Questionvery interesting Pin
totyou131-Feb-13 5:09
totyou131-Feb-13 5:09 
QuestionHave a question Pin
slime1315-Dec-12 5:56
slime1315-Dec-12 5:56 
Generalreporting software Pin
lalalbut30-Aug-12 18:43
lalalbut30-Aug-12 18:43 
GeneralMy vote of 5 Pin
bhalodiyapragnesh5-May-11 7:03
bhalodiyapragnesh5-May-11 7:03 
QuestionI want to display in reportViexer1 report.rdlc data from a xml file with VB.net if possible Pin
elmeksoaui110-Mar-11 12:49
elmeksoaui110-Mar-11 12:49 
NewsHave you checked out the RDLReportViewer control... Pin
Christopher Stratmann16-Apr-10 18:18
Christopher Stratmann16-Apr-10 18:18 
QuestionRemote Report Pin
AcidAndroid17-Dec-08 12:04
AcidAndroid17-Dec-08 12:04 
AnswerRe: Remote Report Pin
DIren17-Dec-08 20:50
DIren17-Dec-08 20:50 
GeneralRe: Remote Report Pin
dg782-Sep-09 4:42
professionaldg782-Sep-09 4:42 
GeneralRe: Remote Report Pin
DIren2-Sep-09 5:36
DIren2-Sep-09 5:36 
QuestionReport is ok but subreport Pin
AcidAndroid12-Dec-08 8:57
AcidAndroid12-Dec-08 8:57 
AnswerRe: Report is ok but subreport Pin
DIren12-Dec-08 9:17
DIren12-Dec-08 9:17 
GeneralRe: Report is ok but subreport Pin
AcidAndroid17-Dec-08 13:32
AcidAndroid17-Dec-08 13:32 
GeneralSome problems Pin
_str_618-May-08 5:44
_str_618-May-08 5:44 
GeneralRe: Some problems Pin
DIren14-May-08 22:30
DIren14-May-08 22:30 
GeneralRe: Some problems Pin
Hari Om Prakash Sharma6-Jul-08 20:30
Hari Om Prakash Sharma6-Jul-08 20:30 
GeneralRe: Some problems Pin
DIren7-Jul-08 0:55
DIren7-Jul-08 0:55 
GeneralRe: Some problems Pin
Hari Om Prakash Sharma7-Jul-08 2:18
Hari Om Prakash Sharma7-Jul-08 2:18 

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

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