Wednesday, August 31, 2011

Asp.Net MVC and Report Viewer Control



public ActionResult GetActiveRecordReports()
        {
            List<PE_getActiveCaseListResult> myResult = new List<PE_getActiveCaseListResult>();
            try
            {
                // myResult = (List<PE_getActiveCaseListResult>)Session["GETACTIVEREPORTDATA"];
                myResult = (List<PE_getActiveCaseListResult>)WebserviceHelper.GetReportData("GETACTIVEREPORTDATA"); //List
                CasesModel model = new CasesModel();
                LocalReport localReport = new LocalReport();
                localReport.ReportPath = Server.MapPath("~/Reports/ActiveCaseReport.rdlc"); //Report Path
                String[] dateFrom = Request.QueryString[1].ToString().Split('K'); //Date
                String[] dateTo = Request.QueryString[2].ToString().Split('K');
                DateTime DateBegin = new DateTime(Convert.ToInt32(dateFrom[2]), Convert.ToInt32(dateFrom[0]), Convert.ToInt32(dateFrom[1]));
                DateTime DateEnd = new DateTime(Convert.ToInt32(dateTo[2]), Convert.ToInt32(dateTo[0]), Convert.ToInt32(dateTo[1]));
                string FileType = Request.QueryString[3].ToString();// File Type "PDF,Excel,TIF

                ReportDataSource reportDataSource = new ReportDataSource("AciveDataSet", myResult); //Dateset
                ReportParameter pFrom = new ReportParameter("DateFrom", DateBegin.ToString("MM/dd/yyyy")); //Parameter
                ReportParameter pTo = new ReportParameter("DateTo", DateEnd.ToString("MM/dd/yyyy"));
                localReport.SetParameters(new ReportParameter[] { pFrom, pTo });


                localReport.DataSources.Add(reportDataSource);
                string reportType = FileType;
                string mimeType;
                string encoding;
                string fileNameExtension;
                string deviceInfo = string.Empty;
                Warning[] warnings;
                string[] streams;
                byte[] renderedBytes;
                //The DeviceInfo settings should be changed based on the reportType
                //http://msdn2.microsoft.com/en-us/library/ms155397.aspx

                //Render the report
                renderedBytes = localReport.Render(
                    reportType,
                    null,
                    out mimeType,
                    out encoding,
                    out fileNameExtension,
                    out streams,
                    out warnings);

                Response.AddHeader("content-disposition", "attachment; filename=ActiveCaseReport." + fileNameExtension);
                return File(renderedBytes, mimeType);

            }

            catch (Exception ex)
            {
                employment.CommonClasses.PeErrorLog.InsertErrorLog("User Name:-" + employment.CommonClasses.ControlHelper.GetPeSessionUserName(),
                                                                "GetActiveRecordReports():- " + ex.Message.ToString(), " Address:-" + Request.Path);
            }

            return null;
        }