Getting exception on refreshing the Crystal Report

2019-08-07 06:05发布

I am using Crystl Report XI and WFP viewer to view the report on a window.

I am trying to refresh my Crystal Report using the RefreshReport() method. But it gets a NULLReferanceException at the line Me.irRapportViewer.ViewerCore.RefreshReport().

If i remove this line the report gets loaded but its not the latest one. To get the latest one i have to click on the Refresh button on the viewer. Thats why I need to refresh it from the code.

Bellow is the code. I can't find what I am doing wrong.

Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

        Dim cryRpt As New ReportDocument()
        Dim crtableLogoninfos As New TableLogOnInfos()
        Dim crtableLogoninfo As New TableLogOnInfo()
        Dim crConnectionInfo As New ConnectionInfo()
        Dim CrTables As Tables

        Dim startupPath As String = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName
        cryRpt.Load(startupPath & "\Reports\IrReport2.rpt")

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("ReportConnStr").ConnectionString
        Dim conn = New SqlConnectionStringBuilder(connectionString)

        crConnectionInfo.ServerName = conn.DataSource
        crConnectionInfo.DatabaseName = conn.InitialCatalog
        crConnectionInfo.UserID = conn.UserID
        crConnectionInfo.Password = conn.Password

        CrTables = cryRpt.Database.Tables
        For Each CrTable As CrystalDecisions.CrystalReports.Engine.Table In CrTables
            crtableLogoninfo = CrTable.LogOnInfo
            crtableLogoninfo.ConnectionInfo = crConnectionInfo
            CrTable.ApplyLogOnInfo(crtableLogoninfo)
        Next

        Me.irRapportViewer.ViewerCore.ReportSource = cryRpt
        Me.irRapportViewer.ViewerCore.ReuseParameterWhenRefresh = True
        Me.irRapportViewer.ViewerCore.RefreshReport()

    End Sub

The XAML is as bellow

<Window x:Class="IrRptWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cr="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"
    Title="IrRptWindow" Height="700" Width="800" WindowStartupLocation="CenterScreen">
    <Grid>
        <cr:CrystalReportsViewer Name="irRapportViewer" ToggleSidePanel="None"></cr:CrystalReportsViewer>
    </Grid>
</Window>

1条回答
Animai°情兽
2楼-- · 2019-08-07 06:25

After hours of looking here and there finally I thought why not try refreshing the ReportDocument and amazingly it works. I tried cryRpt.Refresh() before setting the report to the viewer. Following are the modified last three lines of my window load event.

cryRpt.Refresh()
Me.irRapportViewer.ViewerCore.ReportSource = cryRpt

'Me.irRapportViewer.ViewerCore.ReuseParameterWhenRefresh = True (No Need)
'Me.irRapportViewer.ViewerCore.RefreshReport() (No Need)

Comments are appreciated under this answer.

查看更多
登录 后发表回答