Friday, 26 December 2014

Create RDLC Report using DataSet in ASP.NET Website

In this Article, We will learn How to create RDLC Report using DataSet in ASP.NET Website / Web Application. In Previous Article, We saw how to create a RDLC Report in ASP.Net using Visual Studio Report Designer. If you missed that, visit the following link:


You have seen that in previous example, We havn't type even a single line of Code. In this Article, we will create RDLC Report using DataSet and type few lines of code for filling the DataSet with Data and use that DataSet as a ReportDataSource.
For demonstration, I have created a database (named as Sample) and created a table tbl_Employee in it. The following is the table design for creating tbl_Employee.
RDLC Reports image1
Add some records to tbl_Employee. I have attached the script of the database along with the source code for download.

Let's Begin:
1. Create New Website.
2. Right Click on Website -> Add New Item -> Select DataSet
3. You will see Empty DataSet. Right Click on the DataSet -> Go to Add -> Select DataTable.
4. Right click on DataTable and Add the Columns name. Column Name must match with the Name of Column added in the the tbl_Employee.
Set the DataType of Each column same as declared in tbl_Employee. Save the DataSet(i.e. dsEmployee).
5. Right Click on Website -> Add new item -> Select Report Wizard -> Click on Add.
6. Now the Report Wizard opens. Select the DataSource as Dataset (i.e. dsEmployee).
7. Drag and drop the required fields from the Available fields into the Values section.
Click on "Next" because we didn't want to display a subtotal in our report.
8. Choose a style for your report then click on "Finish". I have selected Slate as the style for my report.
You will see the report named Report.rdlc is created.
9. Add ScriptManager and ReportViewer Control on a Webform. I have added them on Default.aspx.
Now Go to the Code behind file of Default.aspx Webform.
10. Add System.Data.SqlClient and Microsoft.Reporting.WebForms namespace(contains methods and properties for the ReportViewer Web server control). After that add the following code in Page_Load Event of Default.aspx.
using System;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //set Processing Mode of Report as Local
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            //set path of the Local report
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
            //creating object of DataSet dsEmployee and filling the DataSet using SQLDataAdapter
            dsEmployee dsemp = new dsEmployee();
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");
            con.Open();
            SqlDataAdapter adapt = new SqlDataAdapter("select * from tbl_Employee", con);
            adapt.Fill(dsemp, "DataTable1");
            con.Close();
            //Providing DataSource for the Report
            ReportDataSource rds = new ReportDataSource("dsEmployee", dsemp.Tables[0]);
            ReportViewer1.LocalReport.DataSources.Clear();
            //Add ReportDataSource
            ReportViewer1.LocalReport.DataSources.Add(rds);
        }
    }
}
Build and Run the Application.
Click on the Save Button in the report and save the entire report in PDF / Excel / Word format.
I hope you like it. Thanks.
[Download Source Code via Google Drive]

0 comments:

Post a Comment

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Subscribe us on YouTube

Like us on Facebook

Follow us on Google+