Saturday, August 10, 2019

Microsoft Report in asp.net MVC

In this post, I explain how to use Microsoft Report in MVC 4. Most of the developer use Microsoft Report (rdlc) for generating report  in asp.net application. Here I have explained how we can use Microsoft Report (rdlc) in MVC.
Just follow 10 easy steps and get result.


Prerequisite

I used followings:
  • .Net framework 4.0
  • Entity Framework
  • Sql Server 2008

Steps :

Just follow the steps and get result easily.

Step - 1 : Create New Project

Go to File > New > Project > Select asp.net mvc4 web application > Entry Application Name > Click 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 and insert data for show in report

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.

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 Action for populate data.

Go To Controller > Add your action > write following code and Rebuild your application to get data from Database.
  1. public ActionResult StateArea()
  2. {
  3. using (PopulationEntities dc = new PopulationEntities())
  4. {
  5. var v = dc.StateAreas.ToList();
  6. return View(v);
  7. }
  8. }

Step-6: Add View for show data on page.

Right Click on your Action > Add View > Enter View name > Check Create a strongly-type view > Select your model class > Select Scaffold templete > Select list > Add.
  1. @model IEnumerable<MvcReportViwerApp.StateArea>
  2. @{
  3. ViewBag.Title = "State Area";
  4. }
  5. <h2>State Area - Report</h2>
  6.  
  7. <table>
  8. <tr>
  9. <th> State ID</th>
  10. <th>State Name</th>
  11. <th>Area(KM)</th>
  12. </tr>
  13. @foreach (var item in Model) {
  14. <tr>
  15. <td>@Html.DisplayFor(modelItem => item.StateID)</td>
  16. <td>@Html.DisplayFor(modelItem => item.Statename)</td>
  17. <td>@Html.DisplayFor(modelItem => item.Area)</td>
  18. </tr>
  19. }
  20. </table>
  21. <div style="padding:10px; border:1px solid black">
  22. <div><a href="@Url.Action("Report",new {id= "PDF"})"> Get Report PDF</a></div>
  23. <div><a href="@Url.Action("Report",new {id= "Excel"})"> Get Report Excel</a></div>
  24. <div><a href="@Url.Action("Report",new {id= "Word"})"> Get Report Word</a></div>
  25. <div><a href="@Url.Action("Report",new {id= "Image"})"> Get Report Image</a></div>
  26. </div>

Run Application.

Look Result show in your browser.
Now Add some links to your view for generate PDF,Excel,Word files containing report data. Add this to your view.

Step-7: Add Reference (Microsoft.ReportViwer.WebForms.dll)


Right Click on references under project folder > Add Reference > Select Microsoft.ReportViwer.WebForms.dll > OK.

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

Add report folder to your project
Right Click on report folder > Add > New item > Select Report under Reporing > Enter report file name > Add.
Here we also have to add Datasource for our report.
Under report data Click on New > Dataset > New > Choose Data Connection > Next > Select Table > Finish.
Now Design your Report looks.

Step-9: Add Action for generate PDF, Excel, Word and Image File for Report Data

Right Click on your Action > Add View > Enter View name > Check Create a strongly-type view > Select your model class > Select Scaffold templete > Select list > Add. Go To Controller > Add your action > write following code and Rebuild your application to get data from Database.
  1. public ActionResult Report(string id)
  2. {
  3. LocalReport lr = new LocalReport();
  4. string path = Path.Combine(Server.MapPath("~/Report"), "ReportStateArea.rdlc");
  5. if (System.IO.File.Exists(path))
  6. {
  7. lr.ReportPath = path;
  8. }
  9. else
  10. {
  11. return View("Index");
  12. }
  13. List<StateArea> cm = new List<StateArea>();
  14. using (PopulationEntities dc = new PopulationEntities())
  15. {
  16. cm = dc.StateAreas.ToList();
  17. }
  18. ReportDataSource rd = new ReportDataSource("MyDataset", cm);
  19. lr.DataSources.Add(rd);
  20. string reportType = id;
  21. string mimeType;
  22. string encoding;
  23. string fileNameExtension;
  24.  
  25.  
  26.  
  27. string deviceInfo =
  28.  
  29. "<DeviceInfo>" +
  30. " <OutputFormat>" + id + "</OutputFormat>" +
  31. " <PageWidth>8.5in</PageWidth>" +
  32. " <PageHeight>11in</PageHeight>" +
  33. " <MarginTop>0.5in</MarginTop>" +
  34. " <MarginLeft>1in</MarginLeft>" +
  35. " <MarginRight>1in</MarginRight>" +
  36. " <MarginBottom>0.5in</MarginBottom>" +
  37. "</DeviceInfo>";
  38.  
  39. Warning[] warnings;
  40. string[] streams;
  41. byte[] renderedBytes;
  42.  
  43. renderedBytes = lr.Render(
  44. reportType,
  45. deviceInfo,
  46. out mimeType,
  47. out encoding,
  48. out fileNameExtension,
  49. out streams,
  50. out warnings);
  51. return File(renderedBytes, mimeType);
  52. }

Step-10: Run Application

Click on links and get your report in PDF, Excel, Word and Image format.

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 { ...