Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Create RDLC Report in Asp.net MVC 3 using Dataset with Example in C# in Visual Studio 2010

Dec 15, 2014
Introduction:

Here I will explain how to create and use RDLC report in asp.net MVC 3 using dataset with example in c# in visual studio 2010. Normally we use the RDLC report in asp.net web forms application. Similar way we can use the RDLC report in asp.net MVC also. This post will show you how to use RDLC report in Asp.net MVC 3 applications.

Description:

In previous articles I explained how to create rdlc report in asp.net, pass parameters to rdlc report with example in asp.net and many more articles related to rdlc reports, asp.net. Now I will explain how to create RDLC reports in asp.net MVC 3 using dataset with example in c# in visual studio 2010.

What is RDLC?

Full meaning of RDLC is Report Definition Language Client-side. It is the extension of report file. It is used to create report s using Microsoft reporting technology. These files are created by the Visual Studio 2005 version of Report Designer. RDLC reports can be executed directly by the Report Viewer control in client side

Please follow the steps given below to create RDLC Report

Before start implementation first design new table in your database and give name EmployeeMaster

Column Name
Data Type
EmpId
Int(set identity property=true)
EmpName
Varchar(50)
Designation
Varchar(50)
Location
Varchar(50)
EmpStatus
Bit

After completion of table creation enters some dummy data because we need to use that data to populate reports.

Create a Asp.net MVC 3 application


Now add new controller for that right click on controller folder and select Add Controller and give name as Employee like as shown below


Once we add controller next step is we need to add view for that right clicks on action result like as shown below


Now open the view page (cshtml), in which we are displaying the report. In the example I am adding report in ManageEmployees/Index. So I have opened Views\ManageEmployees \Index.cshtml

Now add an iframe in Index.html page like as shown below


<iframe id="myReport" width="100%" height="450px" src="../../Report/ReportDataViewer.aspx?ReportID=EmpListReport">
</iframe>
Now the complete code of index.cshtml looks like this


Now we need to create a model for employee and a model service class to retrieve employee information from database.

Create a class EmployeeMaster in Model folder


Our EmployeeMaster class should contain code like as shown below


namespace MVChero.Models
{
    public class EmployeeMaster
    {
        public int EmpId { get; set; }
        public string EmpName { get; set; }
        public string DesignationName { get; set; }
        public string Location { get; set; }
        public Boolean EmpStatus { get; set; }
    }
}
Now create a class EmployeeModelService in Model folder


Our EmployeeModelService class should contain code like as shown below


namespace MVChero.Models
{
public class EmployeeModelService
{
public List<EmployeeMaster> GetEmployeeInfo(StringBuilder sb)
{
List<EmployeeMaster> objEmpList = new List<EmployeeMaster>();
using (SqlCommand scmd = new SqlCommand())
{
scmd.Connection = scon;
scmd.CommandType = CommandType.Text;
scmd.CommandText = "select EMP_ID,EMP_Name,Desg_Name,Location_Name from Emp_Master inner join Designation_Master on Emp_Master.Desg_ID = Designation_Master.DESG_ID " +
"inner join Location_Master on Emp_Master.Location_ID = Location_Master.Location_ID  where Emp_Status = '1'" + sb;
scon.Open();
SqlDataReader sdr = scmd.ExecuteReader();
while (sdr.Read())
{
EmployeeMaster objEmployeeInfo = new EmployeeMaster();
objEmployeeInfo.EmpId = Convert.ToInt32(sdr["EMP_ID"]);
objEmployeeInfo.EmpName = sdr["EMP_Name"].ToString();
objEmployeeInfo.EmpStatus = true;
objEmployeeInfo.DesignationName = sdr["Desg_Name"].ToString();
objEmployeeInfo.Location = sdr["Location_Name"].ToString();
objEmpList.Add(objEmployeeInfo);
}
return objEmpList;
}
}
}
}
Create a folder “Reports” (or any name) in application root folder. Now let’s create our RDLC report. I assume that you are already aware about creating and using RDLC reports otherwise check this link Create RDLC report in asp.net with example. Now Right click on “Reports” folder and add new Report as shown below.


Now Add Report Dataset (select EmployeeMaster table from your database)


Now select data source as Model folder and choose GetEmployeeInfo from available datasets, which is added earlier in the EmployeeModelService class. Then it will show fields in right hand side.


Now design the report, and our RDLC report will look like, as shown below


Now add new form “ReportViewer.aspx” in Reports folder. It is required in view page (index.cshtml) for displaying report.

Add report viewer control and a script manager in ReportViewer.aspx page as shown below

Now let’s add the code to set datasource for the report. Write the following code in PageLoad event of ReportViewer.aspx.cs file as shown below


public partial class ReportDataViewer : System.Web.UI.Page
{
EmployeeModelService objEmpModelService = new EmployeeModelService();
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
if (Request.QueryString["ReportID"] != null)
{
string reportID = Request.QueryString["ReportID"].ToString();
switch (reportID)
{
case "EmpListReport":
rvDataViewer.Reset();
StringBuilder sb = new StringBuilder("");
DataTable dt = ConvertListToDataTable(objEmpModelService.GetEmployeeInfo(sb));
rvDataViewer.LocalReport.ReportPath = "Report/DataReport.rdlc";
rvDataViewer.LocalReport.DataSources.Clear();
rvDataViewer.LocalReport.DataSources.Add(new ReportDataSource("EmpDataSet", dt));
rvDataViewer.DataBind();
rvDataViewer.LocalReport.Refresh();
break;
default:
break;
}
}
}
}
catch (Exception ex)
{
}
}
static DataTable ConvertListToDataTable(List<EmployeeMaster> empList)
{
// New table.
DataTable table = new DataTable();
table.Columns.Add("EmpId");
table.Columns.Add("EmpName");
table.Columns.Add("DesignationName");
table.Columns.Add("Location");
table.Columns.Add("EmpStatus");
for (int i = 0; i < empList.Count; i++)
table.Rows.Add(empList[i].EmpId, empList[i].EmpName, empList[i].DesignationName, empList[i].Location, empList[i].EmpStatus);
return table;
}
}
Now run the application then you can see the report as shown below. I have attached the full source code with it.

Demo


Create RDLC Report in Asp.net MVC 3 using Dataset with Example in C# in Visual Studio 2010
Download Attached Sample

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Pradeep said...

thank u sir posting this articlefor rdlc

Unknown said...

Nice Post.Keep Updating sir.

Unknown said...

Please tell me the difference between RDLC Report and Crystal Report. When we should use RDLC Report.

postal inspectors said...

how to pass the run time parameter if we need for where condition in sql query of EmployeModelSevice class

Anonymous said...

Excelente artículo.
Comprobado en Visual Studio 2015 Community.

Unknown said...

But sir how to call myReport Iframe that code not seen in this post,or how to call asp.net page from mvc application

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.