Display Crystal Report in MVC4 Razor view: Here I am going to explain you the crystal report implementation in MVC4 razor view. As there is a separate Report Viewer control in Asp.Net to display crystal report on the web page. But in MVC4 there is no report viewer or any HTML helper to display the report on the web page. Here I am displaying the report inside the iframe html element. Here I implemented the report viewer in three ways. web Page Design : Following code is used to Design the web page on which I am going to display the report.@using (Html.BeginForm("Index", "myController")) { <table align="center"> <tr> <td> Select Year: @Html.DropDownList("Year", new List<SelectListItem> { }, new { Id = "ddlYear", @class = "DropDown" }) </td> </tr> <tr> <td> Select Month: @Html.DropDownList("Month", new List<SelectListItem> { }, new { Id = "ddlMonth", @class = "DropDown" }) </td> </tr> <tr> <td> <a href="@Url.Action("GenerateReport")">Get Pdf</a> <input type="submit" value="Show simple" /> <input type="button" value="Show Report" onclick="SimpleInSameWin()" /> </td> </tr> </table> } <iframe name="theFrame" width="100%" height="100%"></iframe>
As design shows there are two buttons and one link. Link is used to generate the PDF Report. While first button i.e. Show Simple is used to display report on the same web page. While Button Show Report is used to display report on the same web page inside the iframe element. Following script is used to call the controller action and then resultant report will be get displayed inside the iframe. Script which call controller action to display report inside the frame element :function SimpleInSameWin() { window.open("../SalarySlip/ShowSimple?year=" + $('#ddlYear').val() + "&month=" + $('#ddlMonth').val(), "theFrame"); // calling action }
Above Script shows that window.open() method opens the crystal report inside the iframe element named theFrame. Also here I passed form elements value as a query string. Above javscript method is called when user clicks on show Report button. Controller code : In side Controller there are lots of method/actions. Following action is used for PDF report generation. 1. Code for PDF generation : To generate pdf report must specify the exact path of your crystal report and must specify the DataSource value. Note: As a practice I just passed the "dt" as argument you may call any method or write your own code to specify the data source for the crystal report.
public ActionResult GenerateReport(DataTable dt) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["HREntities1"]. ConnectionString); ReportDocument rd = new ReportDocument(); rd.Load(Path.Combine(Server.MapPath("~/Report"), "rpt_report.rpt")); rd.SetDataSource(dt); Response.Buffer = false; Response.ClearContent(); Response.ClearHeaders(); Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); stream.Seek(0, SeekOrigin.Begin); return File(stream, "application/pdf", "rpt_report.pdf"); }
When user clicks on given link then above code gets executed and the PDF will be get generated. 2. Display Report on the Same page inside the iframe element : Following controller action is used to generate the report and display it on the same web page. This action is called from javascript method SimpleInSameWin(). This action receive arguments which are passed as querystring from script. Controller action To generate report:public void ShowSimple(string year, string month) { using (ReportClass rp = new ReportClass()) { string emp_code = "120"; rp.FileName = Server.MapPath("~/Report") + "//rpt_Report.rpt"; rp.Load(); /*Get data from detatbase using Data layer via business layer*/ rp.SetDataSource(bizSalarySlip.GetData(month, year, emp_code)); rp.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport"); } }
In above code I passed the argument to the action method and generated the report. As a datasource I called one method by passing arguments. That method returns the DataTable result which is passed as a DataSource. 3. Display Report on whole web page : There may be situation where user want's to display crystal report on the whole page instead of iframe html element. In this situation user must click on button "Show simple" that will call the controller action method name Index and will display report on the same page. Controller Index action method code :[HttpPost] public ActionResult Index(FormCollection formCollection) { string month = formCollection["Month"]; string year = formCollection["Year"]; ; string emp_code = "120"; using (ReportClass rp = new ReportClass()) { rp.FileName = Server.MapPath("~/Report") + "//rpt_Report.rpt"; rp.Load(); rp.SetDataSource(bizSalarySlip.GetData(month, year, emp_code)); rp.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport"); } return View(); }
When user submit the form above action method will be gets called all the form data received as FormCollection object.
Tuesday 16 September 2014
Crystal report in MVC4
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment