In this article I will explain with an example, how to implement RDLC Reports in Visual Studio 2022.
This article makes use of Visual Studio 2022.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Entity Framework Model
Once the
Entity Framework is configured and connected to the database table, the Model will look as shown below.
Controller
The Controller consists of following Action method.
Action method for handling GET operation
Inside this Action method, the Top 10 Customer records are fetched using
Entity Framework and returned to the View.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(from customer in entities.Customers.Take(10)
select customer);
}
}
View
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A FOR EACH loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
There is an HTML Anchor Link which redirects the User to the Reports.aspx (Web Form page discussed later) used for displaying the RDLC Report.
@model IEnumerable<Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h4>Customers</h4>
<hr />
<table cellpadding="0" cellspacing="0">
<tr>
<th>Customer ID</th>
<th>Contact Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@customer.City</td>
<td>@customer.Country</td>
</tr>
}
</table>
<br />
<a href="~/Report.aspx">View Report</a>
</body>
</html>
Configuring RDLC Report in ASP.Net MVC
1. The very first step is to right click on the Project in the Solution Explorer and then click on Add and then New Item.
Then, select Report Wizard from Add New Item dialog and set a name for the RDLC Report and then click on Add.
2. Once you click on Add button, you will need to select the NorthwindEntities from the Data Source Configuration Wizard dialog and then click on Next button.
Note: The
NorthwindEntities is already present in the
Web.Config file as it is used by the
Entity Framework.
3. Then, you will need to check the CheckBox and click Next button.
4. Now you need to choose the Table(s) which will be used to populate the DataSet for the RDLC Report and then click Finish button.
Note: The Tables selected here must be same as the Tables selected while configuring the
Entity Framework.
5. The DataSet is now configured and now we can proceed further by clicking on the Next button.
6. In the Report Wizard dialog, you will need to choose the Fields to be displayed in the RDLC report. This can be done by simple drag and drop of Field from Available fields box to the Values box and then click on Next.
7. This Report Wizard Choose the layout dialog will ask to choose the Layout. It is not needed here and can be skipped as we are not performing any calculations and click on Next.
8. Finally, click on Finish.
Once you click Finish button, your RDLC Report should look as shown below.
9. RDLC Report works only with a RDLC Report Viewer control which is available only in ASP.Net Web Forms and hence, for displaying a RDLC Report, you will need to add a Web Form by right clicking on the Project in the Solution Explorer and click on Add and then New Item and then click on Add.
10. Now in the ASP.Net Web Forms page, you will need to add a RDLC Report Viewer control from the ToolBox.
HTML Markup
The HTML Markup consists of:
ScriptManager – For enabling Ajax.
ReportViewer – For generating report.
<%@Page Language="C#" AutoEventWireup="true" CodeBehind="Report.aspx.cs" Inherits="RDLC_Report_MVC.Reports.Report" %>
<%@Register Assembly="Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server"></rsweb:ReportViewer>
</form>
</body>
</html>
Namespaces
You will need to import the following namespace.
using Microsoft.Reporting.WebForms;
Populating the RDLC Report using Entity Framework
Inside the
Page Load event handler, the RDLC Report is populated with the records of the database using
Entity Framework.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
NorthwindEntities entities = new NorthwindEntities();
ReportDataSource datasource = new ReportDataSource("Customers", (from customer in entities.Customers.Take(10)
select customer));
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
}
Screenshots
HTML Grid displaying Entity Framework records
RDLC Report displaying Entity Framework records
Downloads