ReportViewer... "the handle is invalid"



Hi all,

Using the 2.0 framework (no hotfixes) I'm trying to print a report without
previewing it. The code looks like:

public partial class fErrors : Form
{
private DataImportServices.ImportError[] _Errors;

public fErrors()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
ImportErrorBindingSource.DataSource = _Errors;
reportViewer1.RefreshReport();
}

private void reportViewer1_RenderingComplete(object sender,
RenderingCompleteEventArgs e)
{
//
// Report finished let the user choose a printer
//
reportViewer1.PrintDialog();
}
}

The report viewer is initialized as follows:

reportDataSource1.Name = "DataImporter_DataImportServices_ImportError";
reportDataSource1.Value = this.ImportErrorBindingSource;
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportEmbeddedResource =
"DataImporter.rErrorsList.rdlc";
this.reportViewer1.Location = new System.Drawing.Point(251, 11);
this.reportViewer1.Name = "reportViewer1";
this.reportViewer1.Size = new System.Drawing.Size(149, 28);
this.reportViewer1.TabIndex = 3;
this.reportViewer1.Visible = false;
this.reportViewer1.RenderingComplete += new
Microsoft.Reporting.WinForms.RenderingCompleteEventHandler(this.reportViewer1_RenderingComplete);


Roughly speaking the report is just a simple listing of errors... a line
number and error message/description (the errors just happen to be returned
by a webservice... but I don't think that is related to the problem).


When I click the print button (button1) I'm prompted with the select printer
dialog, I do not change anything... just click print (I have a default
printer, a Dell 1710n, Adobe PDF, and a fax printer). The processing dialog
appears... and then boom an exception.

The specific exception is:

System.Reflection.TargetInvocationException was unhandled
Message="Exception has been thrown by the target of an invocation."
Source="mscorlib"
StackTrace:
at System.RuntimeMethodHandle._InvokeMethodFast(Object target,
Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target,
Object[] arguments, Signature sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at
System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry
tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object
obj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at
System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode
code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at
System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at DataImporter.fMain.cmdImport_Click(Object sender, EventArgs e) in
....\My Documents\Visual Studio
2005\Projects\DataImporter\DataImporter\fMain.cs:line 564
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at DataImporter.Program.Main() in ...\My Documents\Visual Studio
2005\Projects\DataImporter\DataImporter\Program.cs:line 21
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

The inner exception is:
System.ComponentModel.Win32Exception "The handle is invalid",
Error code: -2147467259
Source: System.Drawing
Stack trace:
at
System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument
document, PrintEventArgs e)
at
System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint(PrintDocument
document, PrintEventArgs e)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
at Microsoft.Reporting.WinForms.ReportViewer.PrintDialog()
at DataImporter.fErrors.reportViewer1_RenderingComplete(Object sender,
RenderingCompleteEventArgs e) in ...\My Documents\Visual Studio
2005\Projects\DataImporter\DataImporter\fErrors.cs:line 80
at
Microsoft.Reporting.WinForms.ReportViewer.OnRenderingCompleteUI(ProcessThreadResult
result, PostRenderArgs postRenderArgs)

and Target site is :
{Void OnStartPrint(System.Drawing.Printing.PrintDocument,
System.Drawing.Printing.PrintEventArgs)}


This exact error condition is also mentioned in the following posts:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=541808&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=216364&SiteID=1
http://groups.google.sm/group/RS2005/browse_thread/thread/ac870db0bf8cc3db/548ad83c984f3192?hl=it#548ad83c984f3192

One of the posts seemed to suggest the problem might be printer model
specfic... so I tried printing to the Adobe PDF "printer"... worked like a
charm.


So... is this a known problem with the reportviewer? Is there any
workarounds? Is there anything I can do to help pin the problem down
further?

Regards,

Rob M.
--
rmacfadyen_at_gmail_dot_com


.