HOW TO CREATE RDLC REPORT IN MVC

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  Reporting  Mvc 
 793  View(s)
Ratings:
Rate this:
we want to  create rdlc report in mvc how will  do this  please help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: how to create rdlc report in mvc

    Controller

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Data.Entity;
    using System.Data;
    using Microsoft.Reporting.WebForms;
    using System.IO;
    using ForumMVC;
    using ForumMVC.Models;
    using System.ComponentModel;
    namespace ForumMVC.Controllers
    {
    public class HomeController : Controller
    {
    forumEntities1 db = new forumEntities1();
    public ActionResult Index()
    {

    return View();
    }

    public ActionResult PrintPDF()
    {
    LocalReport localReport = new LocalReport();
    localReport.ReportPath = Server.MapPath("~/Graph.rdlc");
    ReportDataSource reportDataSource = new ReportDataSource();
    reportDataSource.Name = "DataSet1";
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();

    var Result = from c in db.CUSTOMER
    join o in db.ORDER on c.CUST_ID equals o.CUST_ID
    join p in db.PRODUCT on o.PRODUCT_ID equals p.PRODUCT_ID
    select new
    {
    Id = c.CUST_ID,
    Name = c.CUST_NAME,
    Date = o.ORD_DATE,
    Product = p.PRODUCT_NAME,
    Price = p.PRICE
    };
    dt = ConvertToDataTable(Result.ToList());
    ds.Tables.Add(dt);
    reportDataSource.Value = ds.Tables[0];
    localReport.DataSources.Add(reportDataSource);
    string reportType = "PDF";
    string mimeType;
    string encoding;
    string fileNameExtension;

    string deviceInfo =
    "<DeviceInfo>" +
    " <OutputFormat>PDF</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 = localReport.Render(
    reportType,
    deviceInfo,
    out mimeType,
    out encoding,
    out fileNameExtension,
    out streams,
    out warnings);
    //Response.AddHeader("content-disposition", "attachment; filename=Data." + fileNameExtension);
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-length", renderedBytes.Length.ToString());
    Response.AddHeader("content-disposition", "inline;filename=Data." + fileNameExtension);
    Response.BinaryWrite(renderedBytes);
    return null;
    }

    public DataTable ConvertToDataTable<T>(IList<T> data)
    {
    PropertyDescriptorCollection properties =
    TypeDescriptor.GetProperties(typeof(T));
    DataTable table = new DataTable();
    foreach (PropertyDescriptor prop in properties)
    table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
    foreach (T item in data)
    {
    DataRow row = table.NewRow();
    foreach (PropertyDescriptor prop in properties)
    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
    table.Rows.Add(row);
    }
    return table;

    }


    }
    }

    index view 
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.9.1.min.js"></script>
    <script>
    $(document).on('click', '.PrintPDF', function (e) {
    var win = window.open('../Home/PrintPDF/', '_blank');
    console.log('click');
    if (win) {
    win.focus();
    } else {
    alert('Please allow popups for this site');
    }
    });
    </script>
    </head>
    <body>
    <input id="btnreportmvc" type="button" value="Generate report"
    class="btn btn-info PrintPDF" style="margin-right: 10px;" />
    </body>
    </html>

    Report Design

    Report 


Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!