1
votes

Long before I started working here a developer wrote a web app in VB.Net in VS 2008 that included a link that creates a report, using Crystal Reports, as a PDF file. It was reported by a user to me that the link generates an error. I am not sure if this has always been a problem and it is just now being reported or if there was a change on the server. This is an application that I have had little involvement with since I have been here and the developer who wrote it is long gone from the company. I haven't worked with Crystal Reports since the late 90's. Anyway enough background here is the error.

I traced everthing right up to the objTemp.Export() method call (where objTemp is an instantiation of the Crystal Reports ReportClass class).

When I run the app locally on my machine in the Visual Studio IDE everything works exactly as it is supposed to. I recompiled and published the app to a separate folder on the production server and then ran my version and I still get the same error as I do in the production version.

The ASP Error page looks like this:

Server Error in '/MyApplication' Application.

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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
    MyApplication.libMyAppFunctions.ExportAndDisplayPDF(Object objTemp) in O:\MyApplication\library\libMyAppFunctions.vb:491
    MyApplication.ViewReport.btnPrint_Click(Object sender, EventArgs e) in O:\MyApplication\aspx\Reports\ViewReport.aspx.vb:1462
    System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +111
    System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +79
    System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


Version Information:Microsoft .NET Framework Version:2.0.50727.3643; ASP.NET Version:2.0.50727.3634

One of the questions I had is that in the error it points to "O:\MyApplication...". That is the network drive where I keep my source code. There is no O: drive mapped to the server. That seems like that could be the source of the error but I just cannot imagine hard coding drive letters being a standard part of Visual Studio's development environment. It has never caused this type of problem with any other app that I have wrote/worked.



* Added 3/19/2013 in Response to Andrew's request for source code
This is the code that is called when the link to create the PDF file is clicked.

Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    Dim d
    Dim objViewReport As ViewReport = New ViewReport(frmView)
    dtReportComments = objViewReport.getDtReportComments

    Dim oRpt As Object

    If (objViewReport.intTimeOfDayID  -1) Then
        oRpt = New rptTOC_DateSpecific()
    Else
        oRpt = New rptTOC_DateAll()
    End If
    oRpt.SetDataSource(dtReportComments)

    'set Season, date and TimeOfDay
    Dim toSeason As CrystalDecisions.CrystalReports.Engine.TextObject = oRpt.ReportDefinition.ReportObjects.Item("txtSeason")
    Dim toTitle As CrystalDecisions.CrystalReports.Engine.TextObject = oRpt.ReportDefinition.ReportObjects.Item("txtViewReportDate")
    Dim toTimeOfDay As CrystalDecisions.CrystalReports.Engine.TextObject = oRpt.ReportDefinition.ReportObjects.Item("txtTimeOfDay")

    toSeason.Text = objViewReport.strSeasonID

    If (objViewReport.intTimeOfDayID  -1) Then
        toTitle.Text = objViewReport.datViewReportDate.ToString("MM/dd/yyyy")
        toTimeOfDay.Text = objViewReport.strTimeOfDay
    Else
        toTitle.Text = "All"
        toTimeOfDay.Text = "All"
    End If

    ExportAndDisplayPDF(oRpt)
End Sub


This is the code that the btnPrint_click method calls


Public Function ExportAndDisplayPDF(ByVal objTemp As Object)
    Dim dNow As DateTime = Now

    Dim strFileName As String = dNow.Ticks & ".pdf"
    'write to a pdf file.
    Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions()
    objTemp.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
    objTemp.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat

    DiskOpts.DiskFileName = HttpContext.Current.Request.PhysicalApplicationPath.ToString & "ReportOutput\" & strFileName

    objTemp.ExportOptions.DestinationOptions = DiskOpts
    Try
        objTemp.Export()
    Catch oRptExcept As Exception
        HttpContext.Current.Response.Write(oRptExcept.Message & "<br><br>" & oRptExcept.InnerException.Message)
    End Try
    HttpContext.Current.Response.Redirect("/MyApplication/aspx/print/Print.aspx?theDestination=" & strFileName)

End Function

Just looking for the solution.

Thanks
Robert

2
Can you post your code?Andrew
This comment box doesn't allow more than 600 characters so I edited my original post and added the code there.Robert Lawson
thx for posting code. I'm wondering if maybe the Crystal DLLs didn't get wiped off the production server. Your theory about the O: mapped drives is sound - that's just a bad practice, but would be easy to troubleshoot / fix.Andrew
I not 100% sure that it is the dll's. I did say in my first post that I was sure if this has always been a problem. I wasn't thinking when I wrote that because last week it seemed to have fixed itself for a day or 2 but then it started crashing again. I was working on trying a few things and publishing my tests to a test folder when I tried the production version and it worked. I backed out all of my changes in the test version and published it again to the test folder. The production version still worked but later that week it broke again and it still is broken.Robert Lawson
That leads me to believe that I did something, even though I was working in a separate folder, that caused it to work and then I am assuming I did something again to break it. I don't know how that could have happened because I made sure that I separated my work from production. I did not accidentally overwrite the production version. The dates of the production files are still the original dates.Robert Lawson

2 Answers

0
votes

The answer to this question had absolutely nothing to do with Crystal Reports. This is what happens when you send a programmer (me) to do a network admins job. Okay, I know, there are a lot of you brainiacs who can do both but I'm not you. I have very little experience in networking. Just enough to get by. Although it was my feeble brain that figured it out.

The way the application works is that it reads data into a table then it sends that data to the Crystal Reports object to be saved as a brand new PDF file. Then after that happens the application opened that new PDF file for viewing. The error complained that the object was not instantiated. To me that says programming problem.

It wasn't a programming problem at all. The folder the new PDF file was being created in did not have write permission for the user group that the web site runs under.

If you don't have write permissions how can you create your PDF? How can you create your PDF if you don't have write permission?

0
votes

An old question, but I had a similar problem the last few days.

The generic "Object reference not set ... " is very misleading. As usual, the published code will work fine on the DEV and QA environments and will fail in PRODUCTION.

I changed objTemp.Export() to objTemp.ExportToHttpResponse(FileName, Response, false, "") and it worked like a charm.

Hope this helps someone.