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.
2. Right Click on Project go to
Add new item and select dataset.
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:
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.
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.
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.
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.
2. Now add a new column to main
report .rdlc file and drag a Subreport control on to that column.
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.
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.
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.
|