MVC : Create RDLC report - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

MVC : Create RDLC report

In this article I am going to explain how to create RDLC report in MVC application

Description:
I want to show report of State population. I am going to use RDLC report to create and Reportviewer to show report.

Implementation:
Follow the below given steps to create RDLC report in MVC application:

Step 1:
Before create report you have to add Reportviewer to MVC application. To add ReportViewer go to Tools >>  Library Package Manager >> Package manger console and paste the following command. After that hit the enter button.

Install-Package ReportViewerForMvc

After successfully completion of this you will automatically “ReportViewerWebForm,aspx” page will be added to root of project.

Step 2:
Now add RDLC report to project. How to add RDLC report? I have add Rptpopulation.rdlc to project and set datasource for it.

Step 3:

Note :  I am using Database first approach.

Structure of Tb_Population.cs in Model:

namespace MvcApplication4.Models
{
    using System;
    using System.Collections.Generic;
   
    public partial class Tb_Population
    {
        public int Id { get; set; }
        public string StateName { get; set; }
        public Int64 TotalPopulation { get; set; }
    }
}


Step 4:

Add an empty controller to project. I have added ReportsController.cs controller to project and create Actionresult to create report. Don’t forget to import the Reportviewer namespace.

using Microsoft.Reporting.WebForms;

Complete code of Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication4.Models;
using Microsoft.Reporting.WebForms;
using System.Data;

namespace MvcApplication4.Controllers
{
    public class ReportsController : Controller
    {
        DemoEntities db = new DemoEntities();
        //
        // GET: /Reports/

        public ActionResult Index()
        {
            var tb = db.Tb_Population.ToList();
            return View(tb);
        }
        public ActionResult Reports()
        {
            List<Tb_Population> population = new List<Tb_Population>();
            population = db.Tb_Population.ToList();         
            var rptviewer = new ReportViewer();
            rptviewer.ProcessingMode = ProcessingMode.Local;
            rptviewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Rptpopulation.rdlc";
            ReportDataSource rptdatasource = new ReportDataSource("ds", population);
            rptviewer.LocalReport.DataSources.Add(rptdatasource);
            rptviewer.SizeToReportContent = true;
            ViewBag.ReportViewer = rptviewer;
            return View();
        }
      
    }
}

Step 5:

Now add view for Reports action.

Complete Source of View

@using ReportViewerForMvc;

@model MvcApplication4.Models.Tb_Population

@{
    ViewBag.Title = "State population reports";
}

<h4>State population reports</h4>
@if (ViewBag.ReportViewer != null)
{
@Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)
}
<style>
    iframe {
        border-width: 0;
    }
</style>






What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon