AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 Part 2: Using Subreport with Asp.Net Local Report
Posted by Moderator1 on 12/16/2010 7:50:20 AM Category: Asp.Net 2.0
Total Views : 47086
Add to my favorites
Email to friend
  
Introduction
For those who are unfamiliar of how to create an Asp.Net Local Report, kindly read Part 1 to know the basics of creating the Asp.Net local report for web applications. This article is going to explain, how to include a Subreport into the Asp.Net Local report.
Demonstration:

For demonstration purpose, we are going to list out the areas of interest of the customers that listed in Part 1. To achieve this, we need to add a new Report to the project and name it as “Subreport.rdlc”. Follow the same steps to add Data Source, Report Fields and Parameter to the sub-report, which is clearly explained in Part 1. Make sure your DataSet.xsd contains dtCusInterest DataTable prior to proceed the steps below.

Steps to create Subreport

1. Add Data Source to the subreport, from Project Data Source dialog box by choosing “DataSet_dtCusInterest”.

2. Drag and drop a Table control from the Toolbox to the report body section to display the customer interest. A single column in the table control is sufficient, label the header row as “Customer Interest” and bind its detail row as “Cus_Interest”, which can be selected from its Value property.

3. Add a Parameter to the Subreport as “Cus_Code”. This parameter is used to link the Subreport with the main report.

4. Open the Filters property of the table control, set the Expression and the Value as shown below, in order to filter the records by the Customer Code. In the Expression column, choose “Cus_Code” field, Operator as “=” and Value as Parameter field which is “Cus_Code”. Make sure both Expression and Value is of same data type. The screenshot is provided below, for better understanding.

Specify Filters in the subreport
Include Subreport to Main Report

Open the main report, drag and drop Subreport control from the Toolbox to the report body section. Open the Subreport Properties dialog box, in its General tab, in the Subreport dropdown choose the Subreport name. Here the Subreport name is “Subreport” as shown below.

Choose Subreport to include in Main report


Then in the Parameters tab, type Parameter Name as “Cus_Code” and choose the Parameter Value as “Cus_Code” field as shown below.
Set subreport Parameters


Bind Subreport from Code-behind

To bind the Subreport, we need to declare a “SubreportProcessingEvent” event using the SubreportProcessingEventHandler in the Page_Init method as follows,

protected void Page_Init()
{
  ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
}

The below method “FetchCustomerInterest” retrieves data from the database and returns it as DataTable. This DataTable can be used to bind the report.

private DataTable FetchCustomerInterest()
{
  string sql = "Select * from customerinterest"; 
  SqlDataAdapter da = new SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 
  DataTable dt = new DataTable(); 
  da.Fill(dt); 
  return dt;
}


In the LocalReport_SubreportProcessing event, we need to create a ReportDataSource object, 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 interest, which can be retrieved from the FetchCustomerInterest method.


void LocalReport_SubreportProcessing(object sender, 
                                                SubreportProcessingEventArgs e)
{
  ReportDataSource rds1 = new ReportDataSource(); 
  rds1.Name = "DataSet1_dtCusInterest"; 
  rds1.Value = FetchCustomerInterest(); 
  e.DataSources.Add(rds1);
}

That’s it from the coding point of view. Now save the code and report changes. Open the web browser and execute the default page. Click on “Generate Report” button, you can see the report generated in the ReportViewer control, will load the list of customers along with the interest as shown below.

Show Subreport in ReportViewer Control

Related article:
Part 1: Using Asp.Net Local Report (RDLC) in Web Applications
You need to Login or Register to download Source Code.
 
Viewer's Comments
Posted by sampath on 2/4/2011 7:41:43 AM
Excellent article.grate job .keep it up.
 
Posted by Bindhya on 5/18/2011 11:43:35 PM
Great job..
 
Posted by krushna on 3/14/2012 7:37:57 AM
Great Article..It exactally tells how to deal with subreports !!
 
Posted by pratik on 10/24/2012 2:29:54 PM
in vs 2010.... my sub report giving same result for every parent records...plz help me...
 
Posted by hhvjgvj on 2/12/2013 1:34:06 AM
sdfs
 
Posted by hhvjgvj on 2/12/2013 1:34:41 AM
hlo... how r? khana kha keh jana hain!??
 
Posted by sabi on 5/7/2014 12:56:32 AM
Good Article... !! its better if explain in steps by steps
 
 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:
Name:
Email Id:  
We never display your email id anywhere.
Comment/Question: Max. 500 letters
Number Thirty One minus Number One Equals To
 
Sponsored by
Sexy Web Design - Free 71 Page Preview!
FierceDeveloper
No Nonsense XML Web Development With PHP - Free 146 Page Preview!