In this article I am going to explain how to create and pass
parameter to RDLC report in MVC application.
Description:
I want to show report of State population and users can
filter the search for particular state. 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. Also create Post ActionResult to filter according to statename. 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;
ReportParameter param = new ReportParameter();
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();
}
[HttpPost]
public ActionResult Reports(string name)
{
List<Tb_Population> population = new List<Tb_Population>();
population = (from p in db.Tb_Population where p.StateName == name
select p).ToList();
var rptviewer = new ReportViewer();
rptviewer.ProcessingMode = ProcessingMode.Local;
rptviewer.LocalReport.ReportPath =
Request.MapPath(Request.ApplicationPath) + @"Rptpopulation.rdlc";
//ReportParameter[] param = new
ReportParameter[1];
//param[0] = new
ReportParameter("statename", name);
//rptviewer.LocalReport.SetParameters(param);
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>
@using (Html.BeginForm("Reports", "Reports", FormMethod.Post))
{
//textbox
to filter the records
<p> State name : @Html.TextBox("name") <input type="submit" value="Search" /></p>
}
@if (ViewBag.ReportViewer != null)
{
@Html.ReportViewer(ViewBag.ReportViewer as
Microsoft.Reporting.WebForms.ReportViewer)
}
<style>
iframe {
border-width: 0;
}
No comments:
Post a Comment