RDLC Report
Hi Friends,
Today I am going to create an MVC project with RDLC reporting service (step by step).
1). Create a Database in SQL server.
2). Create a table in that database.
3). Open Visual Studio an create a new project.
4). Add an edmx file in the Model folder : RightClick on the Model folder in Solution Explorer section and select new item
|
|
and finally, add your edmx file.
5). Then After adding your edmx file then after, go to solution explorer and right click on your main project and add a new folder with the name of "Report".
then after creating Report folder, and again select that folder(Report) and do right-click on it and select new_item and click, then search Reporting service
in open wizard and select Report option on there.
6). Create a table or DataSet on right-click.
7). Design for your display report. like Add Header and Footer section.
8). Open Controller folder in solution explorer section and select & open "HomeController.cs"
and add the code for their actions.
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication15.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public ActionResult Report()
{
using (DataContext db = new DataContext())
{
var v = db.Contacts.ToList();
return View(v);
}
}
public ActionResult GenerateReport(string typeOfReport)
{ LocalReport lr = new LocalReport();
string path = Path.Combine(Server.MapPath("~/Report"), "MyFirstReport.rdlc");
if (System.IO.File.Exists(path))
{
lr.ReportPath = path;
}
else
{
return View("Index");
}
List<Contact> cm = new List<Contact>();
using (DataContext dc = new DataContext())
{
cm = dc.Contacts.ToList();
}
ReportDataSource rd = new ReportDataSource("APCRUDDataSet",cm);
lr.DataSources.Add(rd);
string reportType = typeOfReport;
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo =
"<DeviceInfo>" +
"<OutputFormat>" + typeOfReport + "</OutputFormat>" +
"<PageWidth>8.5in</PageWidth>" +
"</DeviceInfo>";
Warning[] warning;
string[] streams;
byte[] renderedBytes;
renderedBytes = lr.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warning);
return File(renderedBytes, mimeType); ;
}
}
}
9). Create a view for Report method via right click.
@model IEnumerable<MvcApplication15.Contact>
@{
ViewBag.Title = "Report";
}
<h2>Report</h2>
<table>
<tr>
<th>
Name
</th>
<th>
Mobile
</th>
<th>
Address
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mobile)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
</tr>
}
</table>
<div style="padding:10px; border:1px solid black">
<div><a href="@Url.Action("GenerateReport", new {typeOfReport="PDF"})">Get Report PDF</a></div>
<div><a href="@Url.Action("GenerateReport", new {typeOfReport="EXCEL"})">Get Report EXCEL</a></div>
<div><a href="@Url.Action("GenerateReport", new {typeOfReport="WORD"})">Get Report WORD</a></div>
<div><a href="@Url.Action("GenerateReport", new {typeOfReport="IMAGE"})">Get Report IMAGE</a></div>
</div>
10). Add link button on _Layout.cshtml page
Thank you for reading this Article, I hope it'll help you.
Anonymous User
Thank You.