cancel
Showing results for 
Search instead for 
Did you mean: 

Object reference not set to an instance of an object.

former_member1012208
Participant
0 Kudos

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"

Session("myReport").load("c:\reports\ltrv01.rpt")
crLogin.ApplyInfo(Session("myReport"))


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
            myTable.ApplyLogOnInfo(myTableLogonInfo)
        Next
        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
            Next
        Next
    End Sub
End Class

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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.

former_member1012208
Participant
0 Kudos

Well, all reports use the same odbc connection. So I cannot understand why it is not working for this particular report.

0 Kudos

It may be there are too many ODBC connections are open. Make sure you are closing and disposing of the report object once it's done with. This should release any connections the report has open and free up the ODBC connection.

Try using OLE DB to see if it also has the same problem. And try using Process Monitor to watch what is happening also. ODBC trace my give you a clue also.

Thank you

Don

former_member1012208
Participant
0 Kudos

It doesn't matter what I use (ODBC, OLE DB). Error still comes up for this report. I think I found the problem.

I have put the following in form load

If Not IsPostBack Then
    Crystalreportviewerew1.Reportsource = rpt 
End If 

The moment I commented the if condition my report works without any issues. Not sure why it's not working inside the loop.

former_member1012208
Participant
0 Kudos

Could any one help me with this please? The problem is I must put the code inside if not ispostback, other wise even when I browse from page to page, it keeps executing the report and it takes a long time for some reports to process.

former_member1012208
Participant
0 Kudos

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
   rpt.Load("c:\rep\rep01.rpt")
  'Execute logon code. If required by the report.

   Session("myRpt")= rpt
End If

CrystalreportViewer1.ReportSource = Session("myRpt")

Answers (0)