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