Microsoft Report in MVC 4

In this post I am explain how to use Microsoft Report in MVC 4. Most of the developer use Microsoft Report (rdlc) for generating report  in asp.net application. Here I have explained how we can use Microsoft Report (rdlc) in MVC. 

Next Example : How to display rdlc report in ReportViewer control into an MVC web application.
Top 10 State - based on area
State ID State Name Area(KM)
1 Rajasthan 342269.00
2 Madhya Pradesh 308252.00
3 Maharashtra 307713.00
4 Andhra Pradesh 275045.00
5 Uttar Pradesh 243286.00
6 Jammu & Kashmir 222236.00
7 Gujarat 196024.00
8 Karnataka 191791.00
9 Orrissa 155820.00
10 Chhattisgarh 135194.00
Microsoft Report in MVC 4
 
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCReportViwer.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult StateArea()
        {
            using (PopulationEntities dc = new PopulationEntities())
            {
                var v = dc.StateAreas.ToList();
                return View(v);
            }
        }

        public ActionResult Report(string id)
        {
            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Report"), "ReportStateArea.rdlc");
            if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
            else
            {
                return View("Index");
            }
            List<StateArea> cm = new List<StateArea>();
            using (PopulationEntities dc = new PopulationEntities())
            {
                cm = dc.StateAreas.ToList();
            }
            ReportDataSource rd = new ReportDataSource("MyDataset", cm);
            lr.DataSources.Add(rd);
            string reportType = id;
            string mimeType;
            string encoding;
            string fileNameExtension;

            

            string deviceInfo =

            "<DeviceInfo>" +
            "  <OutputFormat>" + id + "</OutputFormat>" +
            "  <PageWidth>8.5in</PageWidth>" +
            "  <PageHeight>11in</PageHeight>" +
            "  <MarginTop>0.5in</MarginTop>" +
            "  <MarginLeft>1in</MarginLeft>" +
            "  <MarginRight>1in</MarginRight>" +
            "  <MarginBottom>0.5in</MarginBottom>" +
            "</DeviceInfo>";

            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;

            renderedBytes = lr.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);
           

            return File(renderedBytes, mimeType);
        }


        
    }
}



Posted By :