Create RDLC Report Using DataSet in ASP.Net

In my 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 the previous example, we haven't provided even a single line of code. In this article, we will create a RDLC Report using a DataSet and use a few lines of code to fill the DataSet with data and use that DataSet as a ReportDataSource. For demonstration, I have created a database (named Sample) and created a table tbl_Employee in it. The following is the table design for creating tbl_Employee.
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 a new website.

2. Right-click on Website then select Add New Item then select DataSet.


You will see Empty DataSet. Right-click on the DataSet then go to Add then select DataTable.
4. Right-click on DataTable and add the column's name. The column name must match the name of the column added in the the tbl_Employee. 
Set the DataType of each column the same as declared in tbl_Employee. Save the DataSet (in other words dsEmployee). 
5. Right-click on Website then select Add new item then select Report Wizard then click on Add.
6. Now the Report Wizard opens. Select the DataSource as Dataset (in other words 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. Now, add a ScriptManager and ReportViewer Control from the Toolbox on a Webform. I have added them on Default.aspx.  
Now go to the Code Behind file of the Default.aspx Webform.
10. Add the System.Data.SqlClient and Microsoft.Reporting.WebForms namespaces (they contain methods and properties for the ReportViewer Web server control). Then add the following code for the Page_Load event of Default.aspx.
  1. using System;  
  2. using System.Data.SqlClient;  
  3. using Microsoft.Reporting.WebForms;  
  5. public partial class _Default : System.Web.UI.Page  
  6. {  
  7.     protected void Page_Load(object sender, EventArgs e)  
  8.     {  
  9.         if (!IsPostBack)  
  10.         {  
  11.             //set Processing Mode of Report as Local  
  12.             ReportViewer1.ProcessingMode = ProcessingMode.Local;  
  13.             //set path of the Local report  
  14.             ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");  
  15.             //creating object of DataSet dsEmployee and filling the DataSet using SQLDataAdapter  
  16.             dsEmployee dsemp = new dsEmployee();  
  17.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");  
  18.             con.Open();  
  19.             SqlDataAdapter adapt = new SqlDataAdapter("select * from tbl_Employee", con);  
  20.             adapt.Fill(dsemp, "DataTable1");  
  21.             con.Close();  
  22.             //Providing DataSource for the Report  
  23.             ReportDataSource rds = new ReportDataSource("dsEmployee", dsemp.Tables[0]);  
  24.             ReportViewer1.LocalReport.DataSources.Clear();  
  25.             //Add ReportDataSource  
  26.             ReportViewer1.LocalReport.DataSources.Add(rds);  
  27.         }  
  28.     }  
  29. }  
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. 

Up Next
    Ebook Download
    View all
    View all