在WPF中刷新Crystal Report时遇到异常

时间:2012-04-18 10:40:05

标签: wpf crystal-reports

我正在使用Crystl Report XI和WFP查看器在窗口上查看报告。

我正在尝试使用RefreshReport()方法刷新Crystal Report。但它在NULLReferanceException行获得了Me.irRapportViewer.ViewerCore.RefreshReport()

如果我删除此行,则会加载报告但不是最新的报告。要获得最新版本,我必须单击查看器上的Refresh按钮。这就是为什么我需要从代码中刷新它。

贝娄是代码。我找不到我做错了什么。

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

XAML如下:

<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 个答案:

答案 0 :(得分:0)

在这里和那里看了几个小时后,我终于想到为什么不尝试刷新ReportDocument而且令人惊讶的是它有效。在将报告设置给查看者之前,我尝试了cryRpt.Refresh()。以下是我的窗口加载事件的最后三行的修改。

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

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

在此答案中表达了意见。