Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crystal Reports - Value cannot be null. Parameter name: window

I recently came across an unusual error when attempting to load a crystal report form into my WPF application via a dialog, the report would show as loading for a few seconds and then throw an error stating "Value cannot be null. Parameter name: window"

This confused me, as far as i know, crystal reports doesn't use a parameter named window.

This was my code:

A simple window with a CrystalReportsViewer

<Window x:Class="Client.Views.ReportsWindowView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"
    Title="ReportsWindowView" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
    <my:CrystalReportsViewer ShowOpenFileButton="True" Grid.Column="1" x:Name="ReportView"/>
</Grid>

and loading the report from code behind (I've removed the standard ConnectionInfo code for simplicity)

 cryRpt = new ReportDocument();
 cryRpt.Load("report.rpt");
 ReportView.ViewerCore.ReportSource = cryRpt;
like image 205
Ben Avatar asked Jul 09 '14 14:07

Ben


1 Answers

Turns out that Crystal Reports Does indeed use a parameter named window, when it attempts to show an internal error.

CrystalReportsViewer handles internal an error and will try to show a MessageBox:

System.Windows.MessageBox.Show(Window owner, String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage icon)

Show method get u201CWindow owneru201D parameter and CrystalReportsViewer tries pass Owner property CrystalReportsViewer.Owner, however by default owner is null, as such we get this unexpected error.

A simple fix for this, is in the codebehind (even when using mvvm) we simply set the owner to the current window via the following code:

ReportView.Owner = Window.GetWindow(this);

Do this in the OnLoaded Event or similar, and you will find that now you get a messagebox with an internal error thrown by CrystalReportsViewer.

Credit for this solution belongs to this thread

like image 69
Ben Avatar answered Nov 05 '22 08:11

Ben