Skip to Content
Mar 17, 2010 at 08:16 PM

Object reference not set to an instance of an object.


I use the below code to assign the logon information and display crystal reports. With this code except for one report all the other reports work fine. This report uses an sql command. Only difference that I see in this report is, that it takes close to 15 minuets to run. Everything works fine in crystal itself. When I run this in my web page I get the error "Object reference not set to an instance of an object. ". The thing is it prompts me for the parameters and accepts them without any issues, then it takes about 8 to 10 minuets and display the error. Strangly I found out that sometimes it does work, but most times it won't. Could someone help me in this? Any help is greatly appreciated.

'''Note that all report uses an ODBC connection to the SQL server called prod_data

'I use Crystal 2008 with Visual Studio 2005

Dim crLogin As New ApplyCRLogin
crLogin._serverName = "prod_data"
crLogin._userID = "repuser"
crLogin._passWord = "repuser"


Public Class ApplyCRLogin
    Public _dbName As String
    Public _serverName As String
    Public _userID As String
    Public _passWord As String
    Public _integratedSecurity As Boolean

    Public Sub ApplyInfo(ByRef _oRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument)
        Dim oCRDb As CrystalDecisions.CrystalReports.Engine.Database = _oRpt.Database()
        Dim oCRTables As CrystalDecisions.CrystalReports.Engine.Tables = oCRDb.Tables()
        Dim oCRTable As CrystalDecisions.CrystalReports.Engine.Table
        Dim oCRTableLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
        Dim oCRConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo

        oCRConnectionInfo.ServerName = _serverName
        oCRConnectionInfo.UserID = _userID
        oCRConnectionInfo.Password = _passWord
        oCRConnectionInfo.IntegratedSecurity = _integratedSecurity

        SetDBLogonForReport(oCRConnectionInfo, _oRpt)
        SetDBLogonForSubreports(oCRConnectionInfo, _oRpt)
End Sub

Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo, ByVal myReportDocument As ReportDocument)
        Dim myTables As Tables = myReportDocument.Database.Tables
        For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
            Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
            myTableLogonInfo.ConnectionInfo.AllowCustomConnection = True
            myTableLogonInfo.ConnectionInfo = myConnectionInfo
        myReportDocument.DataSourceConnections(0).SetConnection(_serverName, _dbName, _userID, _passWord)
    End Sub

    Private Sub SetDBLogonForSubreports(ByVal myConnectionInfo As ConnectionInfo, ByVal myReportDocument As ReportDocument)
        Dim mySections As Sections = myReportDocument.ReportDefinition.Sections
        For Each mySection As Section In mySections
            Dim myReportObjects As ReportObjects = mySection.ReportObjects
            For Each myReportObject As ReportObject In myReportObjects
                If myReportObject.Kind = ReportObjectKind.SubreportObject Then
                    Dim mySubreportObject As SubreportObject = CType(myReportObject, SubreportObject)

                    Dim subReportDocument As ReportDocument = mySubreportObject.OpenSubreport(mySubreportObject.SubreportName)

                    SetDBLogonForReport(myConnectionInfo, subReportDocument)
                End If
    End Sub
End Class

Edited by: Don Perera on Mar 17, 2010 9:26 PM