How to Create Report using Report Viewer in ASP.NET

1. Add .rdls template in your web application. Go to=>Solution=>Right click on Solution=>Add new item.
 
r.png
 
2. Choose rdlc report from the list of template.
 
r1.jpg
 
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.
 

r2.PNG

5. After that you have go to rdlc file=>Go to Report data=>click on New.
 
r3.jpg

6. After clicking on  New =>add dataset(Mydataset.xsd) that has been created above.
 
r4.png
 
7. Select dataset from Dropdown and click OK.
 
r5.jpg
 
8. After clicking on OK. You can see the dataset added to the rdls file.
 
r6.PNG
 
9. After adding the dataset, design the report using the toolbox control.
 
r7.jpg

 
10. Drag and drop the column from the Report data tab to rdls report file and arrange this according to your own requirement.
 
r6.PNG

 
11. After creating the Report, it will look like :
 
r8.PNG 

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
 
r9.jpg
 
 
Ebook Download
View all
Learn
View all