3

I am developing a .Net application (C#) that views a report (.rdlc) and print it.

In case of successful printing I should update some status in the database.

But the problem I can't find a way to know that the printing is successful.

For reportviewer I have two events:

  • Print Event: It fires when user clicks on print button of reportViewer.

  • PrintingBegin Event:It fires when user clicks OK inside print dialogue - but in case of printer is offline or out of paper...etc, no indication is given and the application won't be able to know.

Is there a way to ensure that the printing process is successful?

Thanks

  • i'm going to redirect you to the following post, it contains information on how to read printer statusses etc... http://stackoverflow.com/questions/1622903/how-do-i-check-if-a-printer-is-installed-and-ready-using-c – Schuere Dec 11 '13 at 12:32
  • 1
    I am not familiar with printer operations, but with a wild guess, If you are giving the print command, you can surround that code with a `try-catch` and check if any exception is thrown. – Tolga Evcimen Dec 11 '13 at 12:35
  • @Schuere even if he knows printer is OK before printing...it doesn't mean that printing won't fail (out of enough paper, any error, printer disconnected). Moreover he won't know which printer user will select (and with which parameters he'll print). – Adriano Repetti Dec 11 '13 at 12:46
  • 1
    @TolgaEvcimen Actually I generate a report in the report viewer and the viewer contains Print button that call windows printing dialogue without my intervention – AbdelRahman Shabana Dec 11 '13 at 15:03
  • I see, I'm sorry for not having any more useful ideas. – Tolga Evcimen Dec 12 '13 at 06:31

1 Answers1

2

It's not so immediate (and you have to handle it in different ways according to how print has been started) but you can do it.

If users click Print button then ReportViewer's OnPrint method will serve request. Any exception will be handled by (unfortunately internal) UpdateUiStatus method. That method (among other things) will raise ReportError event with exception informations. Just add a proper handler for that event and you'll know if print failed. Please note that you won't be notified if printing finished without errors.

Problem with this solution is that you have to understand if it's an event that comes from printer or not (so you have to deal with exception details).

Another way is to hide default print button exposed by ReportViewer control, if you invoke Print method directly then you can catch all exceptions and you'll know when printing started and if it worked or not.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • method that handle the event ReportError of report viewer is never called if printer make an error. Do I miss anything? – AbdelRahman Shabana Dec 11 '13 at 14:59
  • @AbdelRahmanShabana according to source code (version 11)...yes, it should. OnPrint will catch exceptions and tunnel them to UpdateUiStatus. It'll then call OnError (that will raise ReportError). You can check what's going on with debugger, download framework source code and break on exceptions to step function by function. – Adriano Repetti Dec 11 '13 at 15:08