Click here to Skip to main content
15,867,929 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I've written a very basic code of printing as below
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Me.PrintDialog1.PrinterSettings = Me.PrintDocument1.PrinterSettings
    If (Me.PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then

        Me.PrintDocument1.PrinterSettings = Me.PrintDialog1.PrinterSettings
        Me.PrintDocument1.Print()

    End If

End Sub

   Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  MsgBox("Going to print")


    End Sub




This is working as expected, when I write in a new form with just one button to intiate printing, However, when I'm calling this exactly same method in another form, with lot of other functionality (independent of printing piece), it gives following error:

SEHException: "External element has thrown an exception"

The exception comes at the point "Me.PrintDocument1.Print()"

I've tried everything including removing entire code from PrintPage method (as shown above)

Has anybody faced similar issue? There is absolutely no idea on what external element it is talking about.
Please help!!
Posted
Comments
Sergey Alexandrovich Kryukov 15-Apr-13 21:55pm    
Wrong question. We don't need to "face similar issue". You can get help if you provide enough information.

Please provide full exception information. In particular, look at Inner exception and provide full exception information on it, too. Show exception stack, message, type, parameters...
Even better, provide small but complete code sample and steps to reproduce. Use the debugger.
—SA
Vish0512 19-Apr-13 12:05pm    
Hi Sargey,
Thanks for correcting me. I'm giving exception Stack trace below. However, in that trace also, inner exception is not coming (i.e. it is null)

System.Runtime.InteropServices.SEHException was unhandled
ErrorCode=-2147467259
Message="External component has thrown an exception."
Source="System.Drawing"
StackTrace:
at System.Drawing.SafeNativeMethods.StartDoc(HandleRef hDC, DOCINFO lpDocInfo) 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 test.DataEntry.PrintButton_Click(Object sender, EventArgs e) in D:\Projects\test_Reference\test\test\DataEntry.vb:line 958 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(ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at test.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 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()
InnerException:

And the code throwing this exception is same as mentioned above. The code is for a POS application where user will enter billing details (material purchased) and finally generate and print bill. I'm using DatagridView to add Product details.



Also, while debugging, it goes to line Me.PrintDocument1.Print(), However, it does not go to the first line (only line) of printpage method and immediately shows exception.


Please let me know if I shall include more information here.
Kschuler 16-Apr-13 17:26pm    
What are you printing? Is there something different about what is actually being printed? I'd guess the error lies with your report and/or data inside that report.
Vish0512 19-Apr-13 12:08pm    
Hi Kschuler, I'm trying to print data from a DataSet. However, as you can see above, I've given absolutely nothing to print and still getting the same error. And very strangely same code is running from another form in same Project.


1 solution

Maybe this can help???

If Me.PrintDocument1.PrinterSettings.IsValid then
Me.PrintDocument1.Print()
Else
MessageBox.Show("Printer is invalid.")
End If

You're using all printersettings from the print dialog. Could be that some setting is not supported by the printer.

Just an idea, but it doesn't really explain why it works in some cases...
 
Share this answer
 
v3
Comments
Vish0512 19-Apr-13 12:10pm    
Hi Johnny,
Thanks for suggestion. However, it is not working still. My local printer is valid and thus is it calling Print() method however, while debugging it is not even going to the first line of PrintPage method of PrintDocument1.
Kschuler 19-Apr-13 14:00pm    
What about checking other settings on the printer. For example, if it's setup to print landscape by default and your report is portrait. Or if default margins aren't setup the same. You say this same code works in a different form, so I assume it's printing different data. Perhaps there is something about the data that is causing the issue.
Vish0512 20-Apr-13 6:42am    
Hi Kschuler,
I'm printing the same thing in another form. Actually I first wrote code for printing in my main form (To print an invoice with sales data details) and this exception came. Then I made a test form with a button to initiate printing and copied entire printing code in that form and it worked. So there is no difference of printer setting or data or any thing. and the most important thing is that when I'm not printing any thing even then this same exception is coming!!!

Do you have any idea of what "External component" can mean going by exception stack trace which I've shared in this post?

Thanks for your help!!
Kschuler 22-Apr-13 9:30am    
Since the stack trace talks about printing, it would make sense that the external component is the printer. Is printDocument1 a Crystal Report? Or a microsoft report? Are you using a database in this document and if so what kind? I'm also a bit confused. You say you get this same error whe you are not even printing? What are you doing when you get it at other times? Perhaps there is something else going on here.
Johnny J. 22-Apr-13 9:43am    
I googled a little and came up with this:

http://stackoverflow.com/questions/6909795/sehexception-was-unhandled

They seem to have the same problem and there are a couple of suggestions on how to find it. Maybe there's something there that can help you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900