Click here to Skip to main content
15,891,423 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 588.7K   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

 
GeneralReport Pin
madhuri.hyd@cnkonline.com9-Apr-07 23:38
madhuri.hyd@cnkonline.com9-Apr-07 23:38 
GeneralRe: Report Pin
DIren18-Apr-07 1:38
DIren18-Apr-07 1:38 
GeneralTake a long time for report Loading Pin
Arek Suroboyo21-Mar-07 19:38
Arek Suroboyo21-Mar-07 19:38 
GeneralRe: Take a long time for report Loading Pin
DIren22-Mar-07 3:44
DIren22-Mar-07 3:44 
GeneralRe: Take a long time for report Loading Pin
DjallaDK20-Oct-07 11:47
DjallaDK20-Oct-07 11:47 
GeneralOnline Reporting Pin
Sarfaraj Ahmed17-Feb-07 3:39
Sarfaraj Ahmed17-Feb-07 3:39 
GeneralRe: Online Reporting Pin
mnphillips11-Sep-12 3:47
mnphillips11-Sep-12 3:47 
GeneralReport.rdlc problem Pin
Sarfaraj Ahmed6-Feb-07 7:08
Sarfaraj Ahmed6-Feb-07 7:08 
Hi EveryBody
Im using ASP.NET with VS 2005 and trying to make report with Microsoft Reporting Service. Those error Im getting is follows.

1.Report Viewer cant change the report. Example I got a TextBox under a Button where I can put a CustomerID in TextBox. I have put a CustomerID 1001 inside the TextBox and Clicked the Button, 1st Time Im getting the whole informations about that particular Customer. When I have changed the CustomerID like 1002 then !!! inside the code is working properly and the DataSet shows the information about that CustomerID 1002 but ReportViewer cant change the Customer Informations its remain same. Still its showing 1001 informations.

2.A data source instance has not been supplied for the data source 'DataSetAUDITLOG_CountLessons'. I have created 2 tables inside the DataSetAUDITLOG after that I have deleted CountLessons table from DataSET and Im not using this Table anymore and there is no relation with table anywhere. So why Im getting this error.

3. On the otherhand I want to send a CustomerID value to another page from a ReportViewer. Could you please tell me. How can I do it? If you have any project example about this please send me.

I need your help
Thanks



Sarfarj Ahmed

GeneralRe: Report.rdlc problem Pin
DIren8-Feb-07 2:10
DIren8-Feb-07 2:10 
GeneralRe: Report.rdlc problem Pin
Sarfaraj Ahmed12-Feb-07 0:46
Sarfaraj Ahmed12-Feb-07 0:46 
GeneralRe: Report.rdlc problem Pin
DIren15-Feb-07 21:24
DIren15-Feb-07 21:24 
GeneralRe: Report.rdlc problem Pin
MikeTokar12-Jun-07 21:32
MikeTokar12-Jun-07 21:32 
QuestionNot working fo rme. Pin
BKunneke18-Jan-07 3:54
BKunneke18-Jan-07 3:54 
AnswerRe: Not working fo rme. Pin
DIren8-Feb-07 1:53
DIren8-Feb-07 1:53 
GeneralGREAT article! Pin
Jorge Parjus26-Dec-06 4:51
Jorge Parjus26-Dec-06 4:51 
GeneralRe: GREAT article! Pin
DIren8-Feb-07 2:11
DIren8-Feb-07 2:11 
QuestionCan't Change Both DataSet and Rdlc File Pin
aldebaran52022-Nov-06 19:35
aldebaran52022-Nov-06 19:35 
AnswerRe: Can't Change Both DataSet and Rdlc File Pin
DIren8-Feb-07 1:41
DIren8-Feb-07 1:41 
Generalthank you! Pin
robocato27-Oct-06 1:18
robocato27-Oct-06 1:18 
GeneralRe: thank you! Pin
DIren31-Oct-06 0:52
DIren31-Oct-06 0:52 
QuestionSome bugs in ReportViewer Pin
georani21-Aug-06 6:32
georani21-Aug-06 6:32 
AnswerRe: Some bugs in ReportViewer Pin
DIren8-Feb-07 1:34
DIren8-Feb-07 1:34 
GeneralGetting Query and dataset frm .rdlc file Pin
BALASINGAM13-Jul-06 23:38
BALASINGAM13-Jul-06 23:38 
GeneralRe: Getting Query and dataset frm .rdlc file Pin
DIren22-Aug-06 1:21
DIren22-Aug-06 1:21 
GeneralIt didn 't work Pin
Bassem Fawzy27-Jun-06 4:23
Bassem Fawzy27-Jun-06 4:23 

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.