ReportViewer... "the handle is invalid"

R

Rob MacFadyen

Hi all,

Posting this again using my shiny new posting alias (so an MS representative
will respond).


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/RS200...cc3db/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.
 
L

Linda Liu [MSFT]

Hi Rob,

In order to narrow down this issue, I would ask several questions.

1. Could you render the report in the reportviewer correctly after you
click on the button1?

2. Do you mean you always fail to print the report from your program no
matter using any printer except for the Adobe PDF printer?

3. How do you run your program? Run it by double-clicking the program on
your local pc or by typing a url which refers to the program in IE(e.g
http://servername/programname.exe)?

4. You could have a try printing a document from Word to see if the
printers you're using work.

You have mentioned that you want to print a local report without previewing
it. To do this, you should use LocalReport object and the
CreateStreamCallback callback function. For more information on how to do
this, you may refer to the following link.

http://msdn2.microsoft.com/en-us/library/ms252091.aspx

Hope this link helps and I am look forward to your reply.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rob MacFadyen

Linda,

Good set of questions, though I'm completely lost by your first question.
Could you try and rephrase, or be more specific?

2. It only fails if I use a physical printer, my Dell 1710n. If I print to
Adobe PDF, Fax printer, or Microsoft Office Document Image Writer it
succeeds.

3. I'm running my program in visual studio, but I did run a test with a
compiled version (double clicked from local filesystem) with same results.

4. I can print to my dell 1710n just fine from word etc.


I have a new "symptom" though. A new exception is being thrown:
"Exception has been thrown by the target of an invocation."
Inner exception:
"Operation is not valid due to the current state of the object."

This is thrown here:

private void reportViewer1_RenderingComplete(
object sender,
Microsoft.Reporting.WinForms.RenderingCompleteEventArgs e)
{
reportViewer1.PrintDialog();
}

If I introduce a timer based delay in the RenderignComplete event handler I
don't get the new exception.

What's odd is that I haven't changed any of the code for the report.... but
now it's throwing a new exception.

Does any of this shed any light?

Regards,

Rob
 
L

Linda Liu [MSFT]

Hi Rob,

Thank you for your response.

My first question is 'Could you display the report in the reportviewer
successfully after you click on the button1?'.

I have looked up in our inner database, but unfortunately I didn't find any
record that the reportviewer has a printing problem on a specified printer.
I have performed a test on the printer in my office and didn't encouter any
problem. The printer I'm using is HP LJ5200 DN.

Could you please have a try changing a physical printer to see if the
problem still exists?

As for the new exception you have mentioned, it may be because the report
hasn't finished loading when the print dialog shows up. You may move the
line of code 'reportViewer1.PrintDialog();' from the reportviewer's
RenderingComplete event handler to somewhere else, e.g. a button's click
event handler.

Please try my suggestion and let me know the result.


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top