|
|
|
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 :
5715 |
|
Adding to your Favorites....
|
|
|
 |
|
|
|
|
|
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.
|
 |
|
|
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.
|
 |
Then in the Parameters tab, type Parameter Name as “Cus_Code” and choose the Parameter
Value as “Cus_Code” field as shown below.
|
 |
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.
|
|
|
 |
Related article:
Part 1: Using Asp.Net Local Report (RDLC) in Web Applications
|
|
You need to Login or Register to download Source Code.
|
|
|
|
|
|
|
|
|
|
|
|