2

I'm trying to show a Print dialog, before printing in a WPF application. I'm using Microsoft sample code from this link

// Create the print dialog object and set options
PrintDialog pDialog = new PrintDialog();
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;

// Display the dialog. This returns true if the user presses the Print button.
Nullable<Boolean> print = pDialog.ShowDialog();

It crashes on the last line with the following exception:

PrintTicket provider failed to bind to printer. Win32 error: The printer name is invalid.

I tried searching for a solution, and the two that people suggest are installing Printer Server feature, and switching to Any CPU build, but neither of those worked in my case.

Eternal21
  • 4,190
  • 2
  • 48
  • 63
  • Does that happen with every printer you've tried? I've had issues that were printer specific; so that's something to look out for too. – Kcvin Apr 02 '15 at 18:53

1 Answers1

5

I created another blank project, with just the code from Microsoft, and it worked fine. I started comparing the two project settings and they were identical. In the end it turned out to be the following setting causing the problem:

Debug->Exceptions...-Common Language Runtime Exceptions

I forgot I had it on when troubleshooting something else. Once I unchecked it, my program no longer breaks on the exception, and displays the dialog properly.

Eternal21
  • 4,190
  • 2
  • 48
  • 63
  • For those that have read this--there are some low level exceptions that get thrown at the system level that you may only see when you are catching all exceptions. My general rule of thumb is, if the printer prints what you're expecting, then don't fret about exceptions thrown in something like mscorlib. I personally was seeing ThreadSafeHandle exceptions in my application. – Kcvin Apr 02 '15 at 18:51
  • In other words, the answer is "Don't Panic"? – Austin Mullins Aug 21 '15 at 20:03