1. Add .rdls template in your web application. Go
to=>Solution=>Right click on Solution=>Add new item.
2. Choose rdlc report from the list of template.
3. Write some code for data filling purpose
and for creating .xsd file
Here, I have to create dataset.xsd file at
specified location .After creating Mydataset.xsd file
you have add mydataset.xsd file in your web
application.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
public
partial class
_Default : System.Web.UI.Page
{
///
<summary>
///
In page load i have to write the code:this code generate
///
dataset.xsd file at specified location.
///
after that i have to run this application after runung this
///
application .xsd file created at specified location.
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected void
Page_Load(object sender,
EventArgs e)
{
DataTable dt = Getdata();
dt.TableName =
"mytable";
dt.WriteXmlSchema(@"D:\mydataset.xsd");
}
///
<summary>
///
Here i have to create method that return datatable with value
///
</summary>
///
<returns></returns>
private
DataTable Getdata()
{
DataTable _dt =
new DataTable();
_dt.Columns.Add("id");
_dt.Columns.Add("Name");
_dt.Columns.Add("Age");
_dt.Columns.Add("Salary");
_dt.Rows.Add("1",
"Priti", "22",
"544566");
_dt.Rows.Add("2",
"anu", "21",
"7475");
_dt.Rows.Add("3",
"neha", "24",
"4364");
_dt.Rows.Add("4",
"aman", "21",
"4353453");
_dt.Rows.Add("5",
"rakhi", "34",
"34544");
_dt.Rows.Add("6",
"priyanka",
"24", "435435");
return _dt;
}
}
4. After adding Mydataset.xsd file in your web application you can see that the
mydataset.xsd file gets filled with datatable column which has been created in the above
code.
5. After that you have go to rdlc file=>Go to Report data=>click on New.
6. After clicking on New =>add dataset(Mydataset.xsd) that has been created
above.
7. Select dataset from Dropdown and click OK.
8. After clicking on OK. You can see the dataset added
to the rdls file.
9. After adding the dataset, design the report using the
toolbox control.
10. Drag and drop the column from the Report data tab
to rdls report file and arrange this according to your own requirement.
11. After creating the Report, it will look like
:
12. After creating all the above task: Now I am showing you how to use This Report in
your web application?
=>Add reportviewer control on your web page
=>Add script manager control from tool box to
your web form.
=>add Microsoft.ReportViewer.WebForms
assembly =>Right click on your web solution=>add reference
13. After that add this code=>
Write this code on page load of web form:
protected
void Page_Load(object
sender, EventArgs e)
{
//Comment
this code:
//DataTable
dt = Getdata();
//dt.TableName
= "mytable";
//
dt.WriteXmlSchema(@"D:\mydataset.xsd");
if
(!IsPostBack)
{
//This
code write only for getting data from datatable on the basis of id.
//and
pass it to th Generatereport method.
//Here
my data source is my datatable but you can also use other datasource.
DataTable
ddt = new
DataTable();
ddt =
Getdata().AsEnumerable().Where(p => p["id"].ToString()
== "1").CopyToDataTable();
Generatereport(ddt);
}
}
Add this method to your web form=>
///
<summary>
///
Dynamically add report nad datasource to the reportviewer.
///
This method created for generating report.
///
in this method call rdlc report file and add data source to reportviewer.
///
</summary>
///
<param name="dt"></param>
private
void Generatereport(DataTable dt)
{
ReportViewer1.SizeToReportContent = true;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
ReportViewer1.LocalReport.DataSources.Clear();
ReportDataSource _rsource
= new ReportDataSource("DataSet1",
dt);
ReportViewer1.LocalReport.DataSources.Add(_rsource);
ReportViewer1.LocalReport.Refresh();
}
14. Run your application and see the output