CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Working with Report Viewer Control in Asp.Net

By Suresh Kumar Goudampally
Posted On Dec 06,2010
Article Rating:
Be first to rate
this article.
No of Comments: 9
Category: ASP.Net
Print this article.

Working with Report Viewer Control in Asp.Net


Introduction

Reportviewer control was introduced in VS 2005 and Asp.Net 2.0 used to build and display the reports on the Asp.net application. It comes with namespace Microsoft.ReportViewer.WebForms and mainly it is the component of SSRS to build the local reports and also display the reports published on the Reporting Server. In this article I will be summarizing what are the key features of Reportviewer control with samples.

Processing Modes

The Reportviewer controls runs in two processing modes namely Local and Remote mode. The Processing mode determines whether the report processing occurs locally or on a report server. In local mode the report processing is done locally which means the processing is done on the application server where the asp.net is hosted. In server mode the report processing in done on Reporting server where the report is published, just we need to mention the url of published report.

Designing a sample report and displaying using Reportviewer

Steps

1.      Go to the sample project and right click and select Add - New Item - Select report template and Add.  A .rdlc file is added on the project now.

                  Report Viewer Control in Asp.Net  

2.      Right Click on Project go to Add new item and select dataset.

             Report Viewer Control in Asp.Net

3.      Configure the dataset using wizard by selecting a DataTable from the database. Fill the dataset with data using the wizards.

4.      Go to the rdlc file and click on the Data -> Show Datasources, the dataset which was added will be shown as shown in the following figure:

              Report Viewer Control in Asp.Net

5.      Now drag and drop the required fields from datasources->dataset  to .rdlc designer

6.      Next take a sample page and drag the reportviewer control on the page. Make sure the Processing mode is local and give the report path the .rdlc file.

7.      Next take drag and drop ObjectDataSource control onto the same page, and select configure datasource and assign the Dataset you already created as the business object as shown in the below figure.

          Report Viewer Control in Asp.Net

8.      Next configure the Reportviewer control by selecting chose datasource and assign the object datasource control to the report viewer control. Refer the following figure.

Report Viewer Control in Asp.Net

 

9.      Display the data in tabular format and run the application and check the output and the reportviewer displays the report as shown in the following figure.

Report Viewer Control in Asp.Net

 

Binding the data to Reportviewer programmatically using the previously created Report1.rdlc

  ReportViewer1.ProcessingMode = ProcessingMode.Local;

 ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";

 ReportDataSource rdS = new ReportDataSource("DataSet1_students", GetData());

 ReportViewer1.LocalReport.DataSources.Add(rdS);

public DataTable GetData()

{       

 SqlDataAdapter dta = new SqlDataAdapter();

 SqlConnection con = new SqlConnection("Data Source=local;Initial 

 Catalog=master;Integrated Security=True");

 DataSet ds = new DataSet();       

 SqlCommand cmd = new SqlCommand("SELECT studentid AS StudentId, sidno AS 

 SidNo, sname AS SName, Qualification FROM students", con);

 dta.SelectCommand = cmd;

 dta.SelectCommand.Connection = con;

 dta.Fill(ds, "students");

 return ds.Tables[0];

}

Sample showing a subreport for the main record

We can add subreport to the main report using the events provided. Let’s say we need to show the achievements of the students as a subreport for the above demonstrated student report. Here are the steps.

1.      Add new Dataset for the studentachievements records and name it as DsAchievements.xsd. Add a new .rdlc file lets say SubrptAchievements.rdlc and got to the data sources. Drag the achievements column into the report designer.

 Report Viewer Control in Asp.Net

2.      Now add a new column to main report .rdlc file and drag a Subreport control on to that column.

 Report Viewer Control in Asp.Net

3.      For Subreport we need to define parameters to accept as inputs. Go to the subreport .rdlc file and click on Report-> Report Parameters to define the input parameters, give some parameter name.

Report Viewer Control in Asp.Net

4.      Next step is configuring the subreport parameters on the main report. That means from main report we need to pass input parameter value to subreport. The subreport accepts this input and fetches the data related main record. For this we need to main report .rdlc file and click on the subreport control that is dragged and go to properties. A popup window is opened go to the parameters tab; give the parameter name and value.

 Report Viewer Control in Asp.Net

 

5.      Next step is adding the SubReportProcessing event to the ReportViewer control and reading the report parameter based on which the subreport data is fetched.

ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

        void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)

        {

             string strParameter = e.Parameters["StudentId"].Values[0].ToString();

ReportDataSource rdS = new ReportDataSource("DsAchievements_studentachievements",    GetStudentsAchievement(strParameter));

             e.DataSources.Add(rdS);

               

    }

public DataTable GetStudentsAchievement(string strStudentID)

        {

            SqlDataAdapter dta = new SqlDataAdapter();

            SqlConnection con = new SqlConnection("Data Source=local;Initial Catalog=master;Integrated   

            Security=True");

            DataSet ds = new DataSet();

            SqlCommand cmd = new SqlCommand("SELECT Achievement AS Achievements FROM            

            studentachievements where sid='" + strStudentID +"'", con);

            dta.SelectCommand = cmd;

            dta.SelectCommand.Connection = con;

            dta.Fill(ds, "achievements");

            return ds.Tables[0];

        }

6.      Run the application and the output will be displayed as in the following figure. Here the Achievements column is the subreport.

Report Viewer Control in Asp.Net

 




Displaying a report published on the Reporting server

In the previous example we have seen designing a report using .rdlc file and running the Reportviewer on the Local ProcessingMode. There is another processing mode called server using which we can display a report using ReportViewer which is already published on the Reporting server.

Let’s we have report published on the report server and the url is http://localhost/ReportServer/ and the report folder path is SomeFolder and Report name Report1

First we need to set the ProcessingMode as “Server” for the reportviewer control, then we need to assign the given url to the properties in the following way.

 ReportServerUrl  : http://localhost/ReportServer/

 ReportPath       :/SomeFolder/Repor1

Run the page and it displays the report.

Different Controls of the report designer (.rdlc)

Microsoft has given a set of controls to design the reports based on the requirement. Just I will list them and give brief description of each control because exploring each control and providing sample can be an separate topic.

            Textbox – Textbox is used to display the data of single column of business object let’s say dataset or any static data. We can Textbox as a label,           and also display the calculated data of business object using expressions.

            Line – Line is an graphical control which can be dragged to any part of the page and displayed as per the requirement.

            Table – This is used to display the data in the tabular format.

            Matrix - A matrix is a data-bound report item in which data is arranged into columns and rows that intersect at specific data points. Matrices provide functionality similar to cross tabs and pivot tables. Unlike a table, which has a static set of columns, matrix columns can be dynamic.

            Rectangle – Rectangle control is used as a container item for the rest of the controls so that the controls can be easily movable.

            List - List is a data region that you use to display repeating rows data for a single field, or to contain other report items.

            Image – Image is used to display the images which are in the binary form. The image can be embedded in the report or can be retrieved from the     database. To show image we can assign the source as Embedded, External and Database.

            SubReport – This is used to display a report in another report. We can also define parameters to filter data in the subreport.

 Some Important points of ReportViewer Control

1.      The ReportViewer control .rdlc has more significance as it contains the xml schema required for executing the report.

2.      We can use the Expressions for calculating and aggregating the data and display on the report.

3.      ReportViewer has mechanism for implementing sorting, grouping, filtering and applying parameters on the data.

4.      Using ReportViewer we can export the data the files and also print the report.

5.      There are two versions of the controls. The ReportViewer Web server control is used to host reports in ASP.NET projects. The ReportViewer Windows Forms control is used to host reports in Windows application processing.

6.      The ReportViewer control is distributed with Visual Studio 2005 and above. The ReportViewer control is freely redistributable.

Conclusion

This article mainly covers the basics of ReportViewer controls. In future I will cover all the topics of Reportviewer control which include Sorting in report viewer, Showing the bar code, displaying the images from different sources, displaying the chart and demonstrating all the controls of ReportViewer.

References

1.       http://www.gotreportviewer.com/

2.       http://msdn.microsoft.com/en-us/library/ms251671(en-US,VS.80).aspx

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
I can't beivlee I've
I can't beivlee I've been going for years without knowing that. http://xtvtqpudzy.com [url=http://dwvuczeiwke.com]dwvuczeiwke[/url] [link=http://lyiabq.com]lyiabq[/link]
<a href="http://bpbc
<a href="http://bpbckwwqsj.com">Thkiinng</a> like that shows an expert at work
A piece of eruitidon
A piece of eruitidon unlike any other! http://qboydfc.com [url=http://htvapavf.com]htvapavf[/url] [link=http://ezhnuxwilib.com]ezhnuxwilib[/link]
I'd <a href="http://
I'd <a href="http://zoakahi.com">vertune</a> that this article has saved me more time than any other.
I'm rellay into it,
I'm rellay into it, thanks for this great stuff!
Error Occured
Subreport could not be shown.

this error occured and i could not know why ??????
can you help me ???????????
amirelman@yahoo.com
Subreport
SubreportProcessingEventHandler not fired. why?
plz help me
Good
seems to be very clear..Good article!
Good Article
Great article! Thanks Suresh!