Saturday, August 10, 2019

How to display rdlc report in ReportViewer control into an MVC web application

Here I would like to explain how to display RDLC report in ReportViewer control into an MVC web application.

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for show data in report viewer control.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one table as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >


Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a Dataset.

In this example, I have created a folder named "RPTDatasets" for store dataset here.
Right Click on the folder > Add > Add New Item... > Select Dataset under Data > Enter Dataset name > Add.

Step-6: Add Report file(.rdlc) and Design your report.

In this example I have added a folder for store .rdlc files Named "RPTReports"
Right Click on report folder > Add > New item > Select Report under Reporing > Enter report file name > Add.

Step-7: Add a View (aspx) into the Shared Folder

Go to the Folder Views > Shared and Right click on the Folder > Add > View... > Enter View Name > Select ASPX (C#) View Engine > Add.

Step-8: Add asp.net control ScriptManager and ReportViewer In the View (ASPX) for show Report Viewer

Write the following code into the View (ASPX)

  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
  2. <%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
  3. <!DOCTYPE html>
  4. <html>
  5. <head runat="server">
  6. <meta name="viewport" content="width=device-width" />
  7. <title>ReportViwer in MVC4 Application</title>
  8. <script runat="server">
  9. void Page_Load(object sender, EventArgs e)
  10. {
  11. if (!IsPostBack)
  12. {
  13. List<MVCReportViwer.Customer> customers = null;
  14. using (MVCReportViwer.MyDatabaseEntities dc = new MVCReportViwer.MyDatabaseEntities())
  15. {
  16. customers = dc.Customers.OrderBy(a => a.CustomerID).ToList();
  17. ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/RPTReports/rptCustomer.rdlc");
  18. ReportViewer1.LocalReport.DataSources.Clear();
  19. ReportDataSource rdc = new ReportDataSource("MyDataset", customers);
  20. ReportViewer1.LocalReport.DataSources.Add(rdc);
  21. ReportViewer1.LocalReport.Refresh();
  22. }
  23. }
  24. }
  25. </script>
  26. </head>
  27. <body>
  28. <form id="form1" runat="server">
  29. <div>
  30. <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
  31. <rsweb:ReportViewer ID="ReportViewer1" runat="server" AsyncRendering="false" SizeToReportContent="true">
  32. </rsweb:ReportViewer>
  33. </div>
  34. </form>
  35. </body>
  36. </html>

Step-9: Add a new Controller.

In this example I have added "HomeController"
Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Step-10: Add new action into your controller for getting the View for show report.

Here I have added "Index" Action into "Home" Controller. Please write this following code

  1. public ActionResult Index()
  2. {
  3. return View();
  4. }

Step-11: Add view for the Action & design.

Right Click on Action Method (here right click on form action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Add.

  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4. <h2>Our Customer List</h2>
  5. @Html.Partial("ReportViwerASPX")
Step-12: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...