Skip to Content
author's profile photo Former Member
Former Member

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

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Mar 17, 2010 at 09:03 PM

    Check the database connection on the one report not working, if it was configured to ODBC Crystal does not seem allow you to change the database connection at runtime.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      I was able to fix this problem. I used the following code in Form load. Hopefully this will help some one else.

      If Not IsPostBacK Then
         dim rpt as New ReportDocument
        'Execute logon code. If required by the report.
         Session("myRpt")= rpt
      End If
      CrystalreportViewer1.ReportSource = Session("myRpt")

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.