Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 Part 1: Using Asp.Net Local Report (RDLC) in Web Applications
Posted by Moderator1 on 12/16/2010 7:49:09 AM Category: Asp.Net 2.0
Total Views : 70608
Add to my favorites
Email to friend
The inclusion of Local Reports with Asp.Net 2.0 avoids the use of 3rd party reporting tools such as Crystal Report to a web application. Further development in the Reporting Services leverages the usability of the reports in Asp.Net Web Application. Asp.Net report contains rich tools and controls to produce any type of reports that suites all kind of businesses. The report can be easily bound with any data sources using the DataSet object. We can also bring dynamic images into the reports by simply providing the image url from the code-behind.

For simple demonstration, we are going to generate a report to list down few customers in the database. Then in part 2 of this article, we will explain how to include Subreport in the main report to list down the interest of each customer. The customer and their interest will be shown in master-detail format and we are going to display the report in a ReportViewer Control. Besides that, we will place a logo and report title on top of the report, to demonstrate how to show dynamic image and pass parameter values from the code-behind.

Step 1: Add Controls to the Page

In Visual Studio, add an aspx page and add a ReportViewer control in it. Add a Button and change its label as “Generate Report”. This button control will be used to generate report and display the report in the ReportViewer control.
Step 2: Add DataSet to the Project

Add a new DataSet from Solution Explorer > Add New Item dialog box, by default the DataSet1.xsd will be placed inside the App_Code directory. The DataSet will be opened with a DataTable object along with DataTableAdapter methods as shown in the picture.
Add DataSet
Delete this DataTableAdapter collection object and add two new DataTable control, one for Customer List and another for Customer Interest, by dragging and dropping the DataTable control from the Toolbox as shown in the side by picture. Add DataTable control
Rename the default names of the DataTable as “dtCustomers” and “dtCusInterest” and add columns that needs to be displayed in the report as shown in the picture and save the DataSet1.xsd. Add Columns to the DataTable
Step 3: Add Data Source to the Report

Add new Asp.Net Report to the project and add Data Source to the report to place the data display fields in the report.

To open the Report Data Sources dialog box, click Report and then click Data Source. Choose, the Project data sources as “DataSet1_dtCustomers” and click “Add to Report” button.
Add Data Source to the Report
Now we are going to add Customer Name, Customer City and Email Id fields to the report designer. This can be done by simply drag and drop Textbox control from the toolbox to the report body section and set the Value property by choosing the appropriate field names from the dropdown list in the property dialog box.

Step 4: Add Parameter to the Report

To add Parameters to the report, open the Report Parameters dialog box by click on Report menu > Report Parameters. Click the “Add” button and change the Name and Data Type. Add two parameters such as “ReportTitle” and “ReportLogo”. “ReportTitle” will be used to display the title of the report in the page header section. “ReportLogo” parameter will be used to pass the dynamic image URL from the code-behind to the ReportViewer control.
Add Parameter to the Report
To display the report title and an image, we need to add a Textbox and Image control to the report Page Header section. The Textbox and Image control will obtain their values from the parameters “ReportTitle” and “ReportLogo” respectively, from the Value property. To set the value, click on the Value property dropdown, click on “Expression”. The Edit Expression dialog box will open, from the Parameter list, we need to choose the appropriate parameter fields for both the controls. Set Value to Report Parameters
Step 5: Add Source Code in Code-behind

The following method shows how to retrieve the list of customer from the database and keeps in the DataTable object to bind the report.

private DataTable FetchCustomer()
  string sql = "Select * from customers"; 
  SqlDataAdapter da = new SqlDataAdapter(sql, 
  DataTable dt = new DataTable(); 
  return dt;

Step 6: Bind data and Image to the Report

The following code samples how to bind the data in the DataTable to the report using ReportDataSource class. Besides that, it also shows how to pass values to the report parameter from the code behind.

private void BindReport()
  ReportDataSource rds = new ReportDataSource(); 
  rds.Name = "DataSet1_dtCustomers"; 
  rds.Value = FetchCustomer(); 

  ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc"); 

  ReportViewer1.LocalReport.EnableExternalImages = true; 

  ReportParameter rptParam1 = new ReportParameter("ReportTitle", "Asp.Net Local Report Demo"); 
  ReportParameter rptParam2 = new ReportParameter("ReportLogo", 
        "http://" + Request.Url.Host + "/" + ResolveUrl("aspdotnetcodes.gif")); 
  ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rptParam1, rptParam2 }); 


The above codes in BindReport method contain 5 sections which is explained in detail below.
ReportDataSource rds = new ReportDataSource();
rds.Name = "DataSet1_dtCustomers";
rds.Value = FetchCustomer();
The above 3 lines of code, creates a ReportDataSource object. We set its name property as the DataTable name that we have created in the DataSet1.xsd and its value as the DataTable that contains customer list, which can be retrieved from the FetchCustomer method.
ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
The above 3 lines of code is related with ReportViewer control, that assigns the local report physical path to it and add the ReportDataSource object to the Data Sources collection of the local report.

ReportViewer1.LocalReport.EnableExternalImages = true;

The above line declares that the report is going to access an external image from the ReportViewer control.

ReportParameter rptParam1 = new ReportParameter("ReportTitle", "Asp.Net Local Report Demo");
ReportParameter rptParam2 = new ReportParameter("ReportLogo",
       "http://" + Request.Url.Host + "/" + ResolveUrl("aspdotnetcodes.gif"));
ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rptParam1, rptParam2 });

The above 3 lines is used to pass the values to the parameters and add the parameters list to the ReportViewer control.
The above last line of code, causes the local report to render with new dynamic data.

Step 7: Render the Report in the Browser

To render the report in the web brower, call the BindReport method from the click event of the "Generate Report" button.

protected void Button1_Click(object sender, EventArgs e)

Save your code. Run the page in the web browser. Click on the “Generate Report” button, you can see the ReportViewer getting refreshed and report will be generated to show the Customer List.

Show Local Report in ReportViewer Control

Related article: Part 2: Using Subreport with Asp.Net Local Report
You need to Login or Register to download Source Code.
Viewer's Comments
Posted by Amey on 2/16/2011 3:42:55 AM
This Article is Very useful to me.
Posted by Student on 7/7/2012 6:57:09 PM
You say, "To open the Report Data Sources dialog box, click Report and then click Data Source." In Visual Studio 2010 you have to click inside Report Data on the left of the screen where the toolbox resides, click New, choose Dataset, and open the Data source dropdown menu to choose the data source. You say, 'To set the value, click on the Value property dropdown, click on “Expression”. ' There is no Value property for the textbox. I'm stuck there.
Posted by Mohamed Khalil on 12/25/2013 5:29:10 AM
Very Good
 Rating & Comments
A word 'Excellent' means lot to the author of this article. You can give comments about this article but not the author.
Rate this Article:
Email Id:  
We never display your email id anywhere.
Comment/Question: Max. 500 letters
Fifteen plus fifty Equals To
Sponsored by
Speech Technology Magazine
Everything You Know About CSS Is Wrong! - Free 37 Page Preview!
Oracle Magazine