1

I have an application which has index.cshtml page that works correctly. In index, I have the controller below that creates a pdf page :

 public ActionResult Report()
        {
            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Report"), "Person.rdlc");
            if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
            else
            {
                return View("Index");
            }
            List<Person> cm = new List<Person>();

            var viewModel = new PersonIndexData();

            viewModel.People = db.Person
            .Include(k => k.Groups)
            .OrderBy(k => k.Name);

            cm = viewModel.People.ToList();



            ReportDataSource rd = new ReportDataSource("DataSet1", cm);
            lr.DataSources.Add(rd);
            string reportType = "pdf";
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string fileNameExtension = string.Empty;



            string deviceInfo =

            "<DeviceInfo>" +
            "  <OutputFormat>pdf</OutputFormat>" +
            "  <PageWidth>8.5in</PageWidth>" +
            "  <PageHeight>12in</PageHeight>" +
            "  <MarginTop>0.5in</MarginTop>" +
            "  <MarginLeft>0.5in</MarginLeft>" +
            "  <MarginRight>1in</MarginRight>" +
            "  <MarginBottom>0.2in</MarginBottom>" +
            "</DeviceInfo>";

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

            renderedBytes = lr.Render(
                "pdf",
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);


            return File(renderedBytes, mimeType);
        }

When I run the project in Visual Studio, it creates the pdf file. But when I publish project and upload it to server then call the action I get this :

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Line 60:     </tr> 
Line 61:  
Line 62: @foreach (var item in  Model.People)
Line 63: { 
Line 64:     if(item.IsApproved == true) 
     Source File: Index.cshtml    Line: 62

This Error made no sense to me because it gave error in index, which has always worked correctly. Also why does it work on Visual Studio but not on server? Thanks.

jason
  • 6,163
  • 28
  • 94
  • 178
  • Check path. Possibly path doesn't exists .Instead of return view("Index"), try redirectToAction("Index"). – IRONMAN Mar 27 '15 at 08:58
  • I see no Model being passed in `return View("Index");`, do you ever execute that branch in VS? – Henk Holterman Mar 27 '15 at 08:59
  • 1
    The real problem was, there was no path for rdlc, when I added the folder and .rdlc file, it worked. Thanks. – jason Mar 27 '15 at 09:02

0 Answers0