Problem with an instance of a ReportDocument.

mike55

Well-known member
Joined
Mar 26, 2004
Messages
726
Location
Ireland
Problem Solved, had already called the .Dispose method.


Hi all

I have a crystal report which I am displaying in my web page as a .pdf file. I am getting the error message: "Object reference not set to an instance of an object" when the Page_Unload method is fired. Not sure what could be going wrong, as I am using the same code in two other pages, and they work correctly. Is it that I should be using a garbage collector?

Here is the code that I am using:
Code:
Global Variable
Dim oRpt As ReportDocument

Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        oRpt.Dispose()
End Sub

Private Sub GenerateReport()

        Dim crystalServer As String = ConfigurationManager.ConnectionStrings("SERVER").ToString
        Dim count As Int16 = 0
start:
        oRpt = New ReportDocument

        Dim util As New Utility
        Dim requestedHostAddress As String
        requestedHostAddress = Request.UserHostAddress

        If util.GetReportCredentials(dbUser, dbPass, dbName) = False Then
            Response.Redirect("Error.aspx?fc=rpt&type=Msgrpt")
        End If
        Dim rptpath As String = Server.MapPath("rptMessages.rpt")
        If Len(rptpath) = 0 Then
            Exit Sub
        Else
            oRpt.Load(rptpath)
        End If

        oRpt.DataDefinition.FormulaFields("DFrom").Text = "" + Request.QueryString("From") + ""
        oRpt.DataDefinition.FormulaFields("DTo").Text = "" + Request.QueryString("To") + ""
        oRpt.DataDefinition.FormulaFields("Org").Text = "" + Session("OrgID") + ""
        oRpt.DataDefinition.FormulaFields("GID").Text = "" + Request.QueryString("GID") + ""
        oRpt.DataDefinition.FormulaFields("Group").Text = "" + Request.QueryString("Group") + ""
        oRpt.DataDefinition.FormulaFields("Type").Text = "" + Request.QueryString("Type") + ""
        oRpt.DataDefinition.FormulaFields("User").Text = "" + Request.QueryString("by").Replace("", "`") + ""
        oRpt.DataDefinition.FormulaFields("OName").Text = "" + Session("OrgName") + ""

        Try The following try catch statement is responsible for generating the report in a .pdf format.
            Dim crLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
            crLogonInfo = oRpt.Database.Tables(0).LogOnInfo
            crLogonInfo.ConnectionInfo.ServerName = crystalServer
            crLogonInfo.ConnectionInfo.DatabaseName = ConfigurationManager.ConnectionStrings("DBNAME").ToString
            crLogonInfo.ConnectionInfo.UserID = ConfigurationManager.ConnectionStrings("USER").ToString
            crLogonInfo.ConnectionInfo.Password = ConfigurationManager.ConnectionStrings("PASS").ToString
            oRpt.Database.Tables(0).ApplyLogOnInfo(crLogonInfo)

            Dim myExportOptions As CrystalDecisions.Shared.ExportOptions
            Dim myDiskFilesDestinationOptions As CrystalDecisions.Shared.DiskFileDestinationOptions
            Dim myExportFile As String

            myExportFile = "C:\temp\PDF " & Session.SessionID.ToString & ".pdf"
            myDiskFilesDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions
            myDiskFilesDestinationOptions.DiskFileName = myExportFile
            myExportOptions = oRpt.ExportOptions

            With myExportOptions
                .DestinationOptions = myDiskFilesDestinationOptions
                .ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
                .ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
            End With

            oRpt.Export()

            Response.ClearContent()
            Response.ClearHeaders()
            Response.ContentType = "application/pdf"
            Response.WriteFile(myExportFile)
            Response.Flush()
            Response.Close()

            oRpt.Dispose()
            If Not oRpt Is Nothing Then
                oRpt = Nothing
            End If

            System.IO.File.Delete(myExportFile)
        Catch ex As Exception
            If count <= 1 Then
                crystalServer = ConfigurationManager.ConnectionStrings("SERVER_FAILOVER").ToString
                count = +1
            End If
            GoTo start
        End Try
    End Sub

Mike55.
 
Last edited by a moderator:
Back
Top