|
Total Views :
21575 |
|
Adding to your Favorites....
|
|
|
 |
|
|
|
|
|
Introduction |
|
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.
|
|
|
|
|
|
|
Demonstration:
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.
|
|
|
|
|
|
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.
|
 |
|
|
|
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. |
 |
|
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,
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
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.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
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 });
ReportViewer1.LocalReport.Refresh();
} |
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");
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds); |
|
|
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.
|
|
ReportViewer1.LocalReport.Refresh(); |
|
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)
{
BindReport();
} |
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.
|
 |
Related article:
Part 2: Using Subreport with Asp.Net Local Report
|
|
You need to Login or Register to download Source Code.
|
|
|
|
|
|
|
|
|