I have the following code in my form load that loads the reports and assigns it to crystalreportviewer control.
If Not IsPostBack Then
Dim rpt As New ReportDocument
rpt.Load("C:\Inetpub\wwwroot\Portal\Reports\detailvw.rpt")
Dim crLogin As New ApplyCRLogin
crLogin._dbName = ""
crLogin._serverName = "prod_fin"
crLogin._userID = "finprod"
crLogin._passWord = "fin!@#"
crLogin.ApplyInfo(Session("rpt"))
crLogin = Nothing
CrystalReportPartsViewer1 = rpt
End If
Following is the ApplyCRLogin Class
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Web
Imports Microsoft.VisualBasic
Public Class ApplyCRLogin
Public _dbName As String
Public _serverName As String
Public _userID As String
Public _passWord As String
Public _integratedSecurity As Boolean
Public _reportType As Integer 'QHR or Virtuo report
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
For Each oCRTable In oCRTables
oCRTableLogonInfo = oCRTable.LogOnInfo()
oCRTableLogonInfo.ConnectionInfo = oCRConnectionInfo
oCRTable.ApplyLogOnInfo(oCRTableLogonInfo)
Next
oCRTables(0).TestConnectivity()
If Len(Trim(_dbName)) = 0 Then
Else
_oRpt.DataSourceConnections(0).SetConnection(_serverName, _dbName, _userID, _passWord)
End If
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
Now when I run the site it prompts me for the parameters and when I enter them and click on OK, I get the following error.
"Object reference not set to an instance of an object."
But the strange thing is if I comment the "If Not IsPostBack Then" condition, my report works fine. The problem is even when I want to go to the next page in report, the code get executed again and this takes time. Is there any other way to fix this?
Your page is not working -- I am thinking -- due to the nature of the page lifecycle. If you have your db connection strings all setup in a block of code that is run in your page_load event and only run on the first load of the page (not ispostback). Your
variables get set the first time someone loads the page, but on subsequent postbacks, the variables are not set, hence the object reference. If you take out the not ispostback, you get the variables set every time but the page then has to run that code every
time the user takes a postback action on the page.
If you can, I would suggest making some alterations to your class, or making a class that loads your settings before calling your class. Store your data in your web config and use:
System.ConfigurationManager.AppSettings["MyDBUserName"] to pull the proper data. If the data is stored per user, use the httpcontext.current.session object for the user and store the correct data on login.
Thanks for the reply. I tried it and it doesn't work.
Instead of using crystalreportsviewer= reportdocument, I added a crystalreportsource in to the form and in the viewer properties I assign that and loaded the report to crystalreportsource1. Then it works without any issues, but I need to use the reportdocument
object.
Any way that I can assigna reportdocument object to a crystalreportsource? Even this will help me out.
coolvaas1
Participant
938 Points
435 Posts
Object reference not set to an instance of an object.
May 27, 2010 06:09 PM|LINK
I have the following code in my form load that loads the reports and assigns it to crystalreportviewer control.
If Not IsPostBack Then
Dim rpt As New ReportDocument rpt.Load("C:\Inetpub\wwwroot\Portal\Reports\detailvw.rpt") Dim crLogin As New ApplyCRLogin crLogin._dbName = "" crLogin._serverName = "prod_fin" crLogin._userID = "finprod" crLogin._passWord = "fin!@#" crLogin.ApplyInfo(Session("rpt")) crLogin = Nothing CrystalReportPartsViewer1 = rptFollowing is the ApplyCRLogin Class
Imports CrystalDecisions.Shared Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Web Imports Microsoft.VisualBasic Public Class ApplyCRLogin Public _dbName As String Public _serverName As String Public _userID As String Public _passWord As String Public _integratedSecurity As Boolean Public _reportType As Integer 'QHR or Virtuo report 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 For Each oCRTable In oCRTables oCRTableLogonInfo = oCRTable.LogOnInfo() oCRTableLogonInfo.ConnectionInfo = oCRConnectionInfo oCRTable.ApplyLogOnInfo(oCRTableLogonInfo) Next oCRTables(0).TestConnectivity() If Len(Trim(_dbName)) = 0 Then Else _oRpt.DataSourceConnections(0).SetConnection(_serverName, _dbName, _userID, _passWord) End If 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 ClassNow when I run the site it prompts me for the parameters and when I enter them and click on OK, I get the following error.
"Object reference not set to an instance of an object."
But the strange thing is if I comment the "If Not IsPostBack Then" condition, my report works fine. The problem is even when I want to go to the next page in report, the code get executed again and this takes time. Is there any other way to fix this?
Any help is greatly appreciated.
jasonjanofsk...
Participant
1022 Points
213 Posts
Re: Object reference not set to an instance of an object.
May 28, 2010 03:52 AM|LINK
Your page is not working -- I am thinking -- due to the nature of the page lifecycle. If you have your db connection strings all setup in a block of code that is run in your page_load event and only run on the first load of the page (not ispostback). Your variables get set the first time someone loads the page, but on subsequent postbacks, the variables are not set, hence the object reference. If you take out the not ispostback, you get the variables set every time but the page then has to run that code every time the user takes a postback action on the page.
If you can, I would suggest making some alterations to your class, or making a class that loads your settings before calling your class. Store your data in your web config and use:
System.ConfigurationManager.AppSettings["MyDBUserName"] to pull the proper data. If the data is stored per user, use the httpcontext.current.session object for the user and store the correct data on login.
nareshguree2...
Star
11118 Points
1997 Posts
Re: Object reference not set to an instance of an object.
May 28, 2010 04:29 AM|LINK
when you work in Not page.IspostBack then Crystal Report work fine.
but after page life cycle, crystal report other event did not fire due to past back.
when you using this need to put all the code on page load without not postback, And
with Not IsPostback you bind the crystal report on button click.
and try this as well
protected void Page_UnLoad(object sender, EventArgs e)
{
if (mb_objcryRptDoc != null)
{
mb_objcryRptDoc.Close();
mb_objcryRptDoc.Dispose();
}
}
it will be work fine.
coolvaas1
Participant
938 Points
435 Posts
Re: Object reference not set to an instance of an object.
May 28, 2010 01:46 PM|LINK
Thanks for the reply. I tried it and it doesn't work.
Instead of using crystalreportsviewer= reportdocument, I added a crystalreportsource in to the form and in the viewer properties I assign that and loaded the report to crystalreportsource1. Then it works without any issues, but I need to use the reportdocument object.
Any way that I can assigna reportdocument object to a crystalreportsource? Even this will help me out.