This blog is written to address KB 1544651 – Crystal Reports WPF Viewer fails with NullReferenceException when used in a WinForms application and the forum post The WPF CrystalReportsViewer fails with NullReferenceException in WinForms. The issue discussed is “fixed”, however the fix has not been tested and thus is “not supported”. E.g.; we will help you, but in the end your solution may not work.
Getting it done
First thing we need is to ensure that the latest update is applied. See SAP Crystal Reports, developer version for Microsoft Visual Studio: Updates & Runtime Downloads for links to all upates. Next, start new Winform project. Mine is a VB project called myWinWPFViewer2. Set your project to use the Full 4.0framework.
Add a reference to “WindowsFormsIntegration”. Add a reference to reference to “System.Xaml”. Or, if you just place the element control on the form, then these references should be added automatically. If your control is already written, you can add it through the Add Existing Item menu option. If you do not have a control already written, create a new WPF user control that you want to add to your app: Right click on the project name in the Solution Explorer Choose Add | New Item From the Installed Templates, under “Common Items”, select WPF | User Control(WPF) Give your control a name if you wish.
I called mine Your project should now look something like this:We now need to add the Crystal Reports WPF Viewer to the Toolbox In the Toolbox, right click on any white empty space and select “Choose Items…” In the Choose Toolbox Items dialog, click on the WPF Component tab. Select CrystalReportsViewer:
Build your solution now. This will ensure that the WPFCRControl is added and available from the ElementHost below. Go to the Windows form From the Toolbox select Element Host under the WPF Interoperability header, click on the down arrow on the upper right of the ElementHost and select WPFCRontrol
Go to the WPFCRControl.xaml form From the Toolbox, select the CrystalReportViewer. The Viewer will now be placed on the Control. Go to the Viewer properties and enable the ShowOpenFileButton:
Run your app, click on the Open File Button on the WPF viewer and browse to a “Saved Data” report. You should now see the report in the viewer: