Click here to Skip to main content
Licence CPOL
First Posted 28 Mar 2006
Views 337,445
Bookmarked 98 times

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

By | 25 Jul 2006 | Article
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.

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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberbhalodiyapragnesh7:03 5 May '11  
QuestionI want to display in reportViexer1 report.rdlc data from a xml file with VB.net if possible Pinmemberelmeksoaui112:49 10 Mar '11  
NewsHave you checked out the RDLReportViewer control... Pinmemberchris17518:18 16 Apr '10  
QuestionRemote Report PinmemberAcidAndroid12:04 17 Dec '08  
AnswerRe: Remote Report PinmemberDIren20:50 17 Dec '08  
GeneralRe: Remote Report Pinmemberdg784:42 2 Sep '09  
GeneralRe: Remote Report PinmemberDIren5:36 2 Sep '09  
QuestionReport is ok but subreport PinmemberAcidAndroid8:57 12 Dec '08  
AnswerRe: Report is ok but subreport PinmemberDIren9:17 12 Dec '08  
GeneralRe: Report is ok but subreport PinmemberAcidAndroid13:32 17 Dec '08  
GeneralSome problems Pinmember_str_615:44 8 May '08  
GeneralRe: Some problems PinmemberDIren22:30 14 May '08  
GeneralRe: Some problems PinmemberHari Om Prakash Sharma20:30 6 Jul '08  
GeneralRe: Some problems PinmemberDIren0:55 7 Jul '08  
GeneralRe: Some problems PinmemberHari Om Prakash Sharma2:18 7 Jul '08  
GeneralRe: Some problems Pinmembersanzopaol7:51 9 Feb '11  
GeneralRe-loading the report data to the ReportViewer PinmemberAlec MacLean6:10 7 Mar '08  
Hi,
Great article, which has helped tremendously. I did have a problem though, which was a simple fix, if not terribly obvious.
 
I thought it might help anyone else out there using the same technique as me if I posted my approach. (I'm using VB, not C#, but code should be easy to follow.)
 
I have the ReportViewer on a winform, which also has a customer list (a Combobox) and two date fields that provide from-to values (both DateTimePickers). These supply the parameters to a web service, which calls a stored procedure with the supplied parameters and returns a dataset with the report result values.
 
On first running the report, this works exactly as expected.
 
My issue was that on re-running the report - even without changing the parameter values - the report wasn't updating to reflect the new values.
 
Fortunately the fix was simply calling the "Reset" of the ReportViewer before binding it.
 
My code goes like this:
 

Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
Try
'Get the data matching user-specified criteria (date range/customer).
Me.LoadInvoiceReportDataset()
 
'Reset the ReportViewer. Required if performing subsequent report calls.
Me.rvInvoice.Reset()
 
'Set processing mode, report definition file, etc.
Me.SetReportProperties()
 
'Refresh the report content
Me.rvInvoice.RefreshReport()
 
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
 
End Sub
 

Private Sub LoadInvoiceReportDataset()
Try
'Call web service to obtain report dataset.
Me.dsReport = serviceProxy.Finance_PhysioCustomer_Report_Select(CInt(Me.cbxCustomer.SelectedValue), Me.dtpFrom.Value, Me.dtpTo.Value)
 
''####################################################################################
''These steps are only required if the report structure changes and is intended
''for designer use only. Not required for production run-time.
''To create a representation of the report data fields, we create an XSD file
''from the dataset, then manually add it to the project so it shows up in the
''DataSources explorer.
'Me.dsReport.DataSetName = "InvRep"
''Create XML schema for the data
'Dim xw As System.Xml.XmlWriter
'xw = System.Xml.XmlWriter.Create(My.Application.Info.DirectoryPath + "\InvRep.xsd")
'Me.dsReport.WriteXmlSchema(xw)
''When running VS in debug mode, the XSD will appear in the \bin\debug folder.
''Import the new XSD as an "existing file" to the project.
''####################################################################################
 
Catch ex As Exception
MessageBox.Show("Error obtaining report data" & vbCrLf & ex.Message, "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
 
Private Sub SetReportProperties()
'Set properties for the report viewer.
Try
'The path to the RDLC (report layout and binding description). This is
'set to be copied locally with the app (ClickOnce required), so we can use
'the app run location as the path.
Me.rvInvoice.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
Me.rvInvoice.LocalReport.ReportPath = My.Application.Info.DirectoryPath + "\reportInvoiceSummary.rdlc"
Me.rvInvoice.LocalReport.ExecuteReportInCurrentAppDomain( _
System.Reflection.Assembly.GetExecutingAssembly.Evidence)
 
'Bind the report to the viewer
Dim rdsRpt As ReportDataSource
rdsRpt = New ReportDataSource("InvRep_InvoiceReport", Me.dsReport.Tables("InvoiceReport"))
Me.rvInvoice.LocalReport.DataSources.Add(rdsRpt)
 
Catch ex As Exception
MessageBox.Show("Error setting Report viewer properties" & vbCrLf & ex.Message, _
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
 
End Sub

 
Hope that helps someone!
 
Alec
QuestionHow to fill dataset derived from XSD file? PinmemberColizobble1:29 9 Oct '07  
AnswerRe: How to fill dataset derived from XSD file? PinmemberDIren3:07 9 Oct '07  
GeneralRe: How to fill dataset derived from XSD file? PinmemberColizobble3:34 9 Oct '07  
Question3d charts in rdlc files Pinmemberkriskan18:48 4 Sep '07  
AnswerRe: 3d charts in rdlc files PinmemberDIren1:45 5 Sep '07  
GeneralTransform DataSet Xml Schema to RDLC Pinmembernbohr99a15:33 16 Aug '07  
QuestionHow to bind data source in rdlc Pinmemberimran_Shaikh6:24 4 Jul '07  
AnswerRe: How to bind data source in rdlc PinmemberDIren2:00 5 Jul '07  

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.

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