Search code examples
asp.net-mvclocalreport

Why does execution stop on LocalReport.SetParameters call without triggering an error


I have a MVC Controller method that writes the results of an rdlc report to a file. This works fine on my local machine but when it is published to production, it stops execution on the setparameters line and does not trigger an exception. Any clues would be appreciated.

    public JsonResult GenerateSignInSheet(string id)
    {
        try
        {
            string pgmid = id;
            string strProgDate = "";
            DateTime? dtProgDate = null;
            Program prog = db.Programs.SingleOrDefault(p => p.ProgramID == pgmid);
            if (prog != null)
                dtProgDate = prog.ProgDate;
            if (dtProgDate != null)
                strProgDate = String.Format("{0:d}", dtProgDate);

            string strProgLoc = string.Empty;
            var locInfo = this.dc.usp_ProgramLocSiteInfo(id).SingleOrDefault();
            if (locInfo != null) strProgLoc = locInfo.VendorOrHost ?? string.Empty;

            //Rep information
            string strRepName = string.Empty;

            LocalReport localReport = new LocalReport();

            // the following 2 lines will allow globals to work - MS gotcha
            System.Security.PermissionSet sec = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
            localReport.SetBasePermissionsForSandboxAppDomain(sec);

            localReport.ReportPath = Server.MapPath("~/Content/Reports/RptSignInSheetPrefilled.rdlc");
            var sourceList = dc.GetRSVPListForSignin(pgmid).ToList();
            ReportDataSource reportDataSource = new ReportDataSource("DataSetRSVPList", sourceList);
            ReportParameter p1 = new ReportParameter("ProgramID", pgmid);
            ReportParameter p2 = new ReportParameter("ProgDate", strProgDate);
            ReportParameter p3 = new ReportParameter("LocName", strProgLoc);
            ReportParameter p4 = new ReportParameter("RepName", strRepName);

            localReport.SetParameters(new ReportParameterCollection { p1, p2, p3, p4 });
            localReport.DataSources.Add(reportDataSource);

            //string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;

            //The DeviceInfo settings should be changed based on the reportType
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx

            string deviceInfo =

            "<DeviceInfo>" +
            "  <OutputFormat>PDF</OutputFormat>" +
            "  <PageWidth>11in</PageWidth>" +
            "  <PageHeight>8.5in</PageHeight>" +
            "  <MarginTop>0.5in</MarginTop>" +
            "  <MarginLeft>0.5in</MarginLeft>" +
            "  <MarginRight>0.5in</MarginRight>" +
            "  <MarginBottom>0.5in</MarginBottom>" +
            "</DeviceInfo>";

            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;

            //Render the report
            renderedBytes = localReport.Render(
                "PDF",
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);

            string strPDFOutputPath = System.Configuration.ConfigurationManager.AppSettings["PDFDocumentsPath"].ToString();
            string strPDFFilename = strPDFOutputPath + id.Replace("-", string.Empty) + ".pdf";

            System.IO.FileStream fs = new System.IO.FileStream(strPDFFilename, System.IO.FileMode.Create);
            fs.Write(renderedBytes, 0, renderedBytes.Length);
            fs.Close();
            fs.Dispose();


            //var theFile = File(renderedBytes, mimeType);

            return Json(new { File = strPDFFilename });

        }
        catch (Exception ex)
        {
            Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            return Json(new { File = "" });
        }
    }

Solution

  • You should check the version of your dlls.

    I had same problem, I my case (Asp.Net 4 Razor, MVC) needed version 11 of those:

    • Microsoft.ReportViewer.Common
    • Microsoft.ReportViewer.ProcessingObjects
    • Microsoft.ReportViewer.WebForms