Skip to Content

How to reliably set default printer for ReportViewer

We are using CR4VS2010 as part of a WinForms project. The project includes a form that uses the CrystalDecisions.Windows.Forms.CrystalReportViewer control. Our users asked for the viewer to have the viewer default to the last printer used in our application, so we added some code to set the printer name. This allows them to click the "Print" button in the viewer and have the printer automatically set to the specific printer, which may not be their default printer.

The code accesses a CrystalDecisions.CrystalReports.Engine.ReportDocument object. From that, we access the CrystalDecisions.CrystalReports.Engine.PrintOptions object, and from that we set the PrinterName property. This worked well in house. When we made the release available to our user base, our support team has been swamped with users who are getting InvalidSpecifiedPrinter exceptions on the line where we set the PrinterName property.

For some users, the problem is temporarily fixed by logging off and then logging back in their computer. For other users, the problem happens 100% of the time, but only for particular printers. We have tested SP4 and SP6, with the same symptoms on both versions.

I created a very simple test project that easily recreates the symptoms. It has a button that uses the "simple" technique we tried. It has a button that tries to set the printer by using CopyFrom with PrinterSettings and PageSettings classes. It also has a button to print directly to the printer, without involving the print preview control.

The simple button recreates the "invalid printer" error. This test project catches it (something I will be adding to our code as an interim fix).

The "printer settings" button is even less reliable than the simple set of the printer name. It constantly gets "not supported" and "index out of bounds" exceptions when referencing Duplex mode or Collation properties.

The "direct print" button seems to work 100% of the time. This is just there for demonstration, as our users don't always want to print directly to the printer. They want the preview option...

Can anybody offer advice on what I should be doing differently to get the preview control to default to the specific printer? I have attached the sample project source. It's actually a zip file, so you'll have to rename it after downloading it.

Thanks,

- Don Benson -

Tribute Inc.

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    Posted on Sep 25, 2013 at 05:14 PM

    Hi Don,

    For a simple report using default letter size paper this will work because typically the paper size ENUM is 1 for letter size. For custom Paper sizes this becomes a nightmare because when Paper sizes are defiend on each users PC they may have different ENUMS and no way to manage it in CR.

    Also, in your test report you have Dissociate checked on, What this means is there is no printer name saved in the RPT file.

    When using this API to get the saved printer name it's empty:

    rpt.Load("D:\CPP Net 2010\Don Benson\ReportViewerPrinterTest\ReportViewerPrinterTest\Crystalreport1.rpt", CrystalDecisions.Shared.OpenReportMethod.OpenReportByDefault)

    SavePrinterNametxt.Text = rpt.PrintOptions.SavedPrinterName()

    You can get more info about the report printer also. But you are doing this all using the ReportViewer Object. Need to open the report using the engine.

    See if this attached app works for you and you must check off the Dissociate in your reports so CR has a printer name to use and your app can copy the info to the report object.

    Without one it defaults to the end users default printer which could be anything and likely the source of your problems.

    Don


    Add a comment
    10|10000 characters needed characters exceeded

    • Built the app with one small change. Instead of loading the report from disk (a report I did not have), I used the included hierarchical report. It seemed to work fine on my system.

      I copied it to a customer system, and it gets Invalid printer specified errors when I select certain printers. This happens I click the "View" button. Thus, it is having the exact same problem that my test code had on one customer's system.

      Any other ideas? Do you want to see the updated project code I was using to test? I literally changed only two lines in ConfigureCrystalReports:

      private void ConfigureCrystalReports()

      {

      //rpt = new ReportDocument();

      rpt = new Hierarchical_Grouping01();

      //rpt.Load(@"C:\reports\World Sales Report1.rpt");

      crystalReportViewer.ReportSource = rpt;

      }

      No other code was changed.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.