0
votes

I'm developing a crystal report in Visual Studio 2008 that uses a couple of different databases as DataSource. Everything was working fine until I try to navigate to page 2. The code that works (because it has limited results) looks like this

Dim mssqlstr As String
mssqlstr = "SELECT TOP 1 t1.*, t2.column1, t2.column2 FROM 
        tablename1 As t1, tablename2 As t2 WHERE t1.ID = '" & txtID.Text & "'
        AND t2.column2 = RTRIM(LEFT(t1.column_2, 2)) ORDER BY t1.ID DESC"
Dim DAms As New OleDbDataAdapter(mssqlstr, conn)
DAms.Fill(dsQRpt, "tablename")

'The code below is shared by the other subreport functions
QPrpt.Load(Server.MapPath("crreport.rpt"))
QPrpt.SetDataSource(dsQRpt)
crQtrProgress.ReportSource = QPrpt
crQtrProgress.RefreshReport()

But when I need a larger result set from this query

mssqlstr = "SELECT column1, column2 FROM tablename ORDER BY ID DESC"

I get the error

Logon failed.
Details: crdb_adoplus : Object reference not set to an instance of an object.
Error in File C:\Users\ALFRED~1.CAL\AppData\Local\Temp\rptQuarterlyProgress {10667888-35C5-41CA-93EF-214A64741965}.rpt: Unable to connect: incorrect log on parameters."

Both queries use the same connection string and the report fields are coming from drag and dropped fields in a datasheet (.xsd)

I should also mention that the report is using multiple subreports with each subreport coming from a different DataSource. All the subreports are coded similarly and work fine except for when the results have to carry over to the next page. If I limit the number of results then the I get the desired results from each subreport but if the data carries over to another page...kaboom! I get the "Unable to connect..." error.

Also the DataSet connects to the database and displays the table data with no problem. I've been looking for a solution but not finding anything that matches my situation.

Thanks for any help offered

SOLUTION Credit goes to haraman for providing the answer. Here's the working code

    Dim dsQRpt = New Data.DataSet
    Dim QPrpt = New ReportDocument

Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        

        If Page.IsPostBack Then
            If Session.Item("CRpt") IsNot Nothing Then
                QPrpt = Session.Item("CRpt")
            End If
            crQtrProgress.ReportSource = QPrpt
            crQtrProgress.RefreshReport()
        Else
            If Session.Item("CRpt") IsNot Nothing Then
               Session.Remove("CRpt")
            End If
            Session.Add("CRpt", QPrpt)
        End If

    End Sub

Protected Sub btRunReport_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim mssqlstr As String    
        mssqlstr = "SELECT column1, column2 FROM tablename ORDER BY ID DESC"

        Dim DAms As New OleDbDataAdapter(mssqlstr, conn)    
        DAms.Fill(dsQRpt, "tablename")

        'Populate Report 
        QPrpt.Load(Server.MapPath("crreport.rpt"))            
        QPrpt.SetDataSource(dsQRpt)
        crQtrProgress.ReportSource = QPrpt

        Session.Add("CRpt", QPrpt) 

End Sub
1
Are you on WinForms application or WebForms? And which database?haraman
This is a Web Application using a SQL Server databaseCal37

1 Answers

2
votes

It seems CrystalReportViewer loses the ReportDocument on PostBack. You can try saving the ReportDocument in a session and then on PostBack reassign it to the CrystalReportViewer on PageLoad event such as

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)  Handles Me.Load

    QPrpt.Load(Server.MapPath("crreport.rpt"))
    QPrpt.SetDataSource(dsQRpt)

    If Page.IsPostBack Then
        If Session.Item("CRpt") IsNot Nothing Then
            QPrpt = Session.Item("CRpt")
        End If
    Else
        If Session.Item("CRpt") IsNot Nothing Then
            Session.Remove("CRpt")
        End If
        Session.Add("CRpt", QPrpt)
    End If
    crQtrProgress.ReportSource = QPrpt
    crQtrProgress.RefreshReport()
End Sub

Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
    If Session.Item("CRpt") IsNot Nothing Then
        Session.Remove("CRpt")
    End If
    Session.Add("CRpt", QPrpt)
End Sub

In case you have specific problem in setting LogOnInfo then you may also check this SO post Report asking for database login on setting DataTable as DataSource