Designing and Developing Reports in ASP.NET 2.0 - Attaching reports to the ReportViewer control dynamically
(Page 5 of 5 )
Until now you were able to develop reports without really writing a single line of code. When you want to attach different data sources based on specific criteria, you may need to add a few lines of code to your web page.
For this demonstration, I added one more "ReportViewer" control (ReportViewer1) and didn't set any properties at design time. I want to attach a report using a few lines of code. The following is the code used to achieve this:
ImportsMicrosoft.Reporting.WebForms
PartialClass _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.ReportViewer2.LocalReport.ReportPath = "SalesReport.rdlc"
Dim rds As New ReportDataSource
rds.DataSourceId = "ObjectDataSource1"
rds.Name = "SalesData_vSalesPerson"
Me.ReportViewer2.LocalReport.DataSources.Clear()
Me.ReportViewer2.LocalReport.DataSources.Add(rds)
Me.ReportViewer2.LocalReport.Refresh()
End Sub
EndClass
The most important issue to remember from the above code is that the name of the report, data source, must match with the syntax of "<datasetname>_<datatablename>" (SalesData_vSalesPerson).
The above is a very simple code sample used to bind a report to an ObjectDataSource. We can even bind the reports to SELECT statements dynamically (provided the data set and reports are already designed based on the columns retrieved by SQL SELECT statement). I shall cover these aspects in upcoming articles.
Any feedback, problems, suggestions, bugs, errors, improvements etc., are highly appreciated at http://jagchat.spaces.live.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |