[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1238
  • Last Modified:

Select a Printer in ASP.net page

I am trying to print a ReportDocument (Crystal Reports) using the ReportDocument.PrintToPrinter() function. The first parameter that this function needs is the printer name to which the document should be printed. I am trying to use the System.Windows.Forms.PrintDialog class to open up a print dialog and use it to set the printer to which the document should be printed. Here is my code:

ReportDocument rd = LoadReport(litFileName.Text); // Function to generate report document
System.Windows.Forms.PrintDialog pDlg = new System.Windows.Forms.PrintDialog();
pDlg.AllowSomePages = true;
pDlg.ShowHelp = true;

if (pDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
      rd.PrintOptions.PrinterName = pDlg.PrinterSettings.PrinterName;
      int numOfCopies = pDlg.PrinterSettings.Copies;

When I run this code, I get the following error (thrown on the ShowDialog() method call): "It is invalid to show a modal dialog or form when the application is not running in UserInteractive mode. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application."

After doing some web-searching, it seems that you cannot open a Windows.Forms dialog/message box through an ASP.net application.

Is this true? If so, how can I find out the name of the user's default printer in order that I can send this to ReportDocument.PrintToPrinter()??
2 Solutions
You cant control cliend side applicationr or dialogs from asp.net page.

You cant send automatically to the client printer.! It just not possible. User must interact with their internet explorer printer dialog!. They must chooese.

If you could do that, ! Imagine the risk of making asp.net pages!

You will have to make winforms or smart client applications.

jellis613Author Commented:
all I need is the name of the person's default printer. Is there some way that I can parse the list of printers available for this person to use on their computer and then let the user select which printer they would like to print to?
You cannot get anything relating to the printer from Internet Explorer. This is not a coding issue, it is a security one. If any browser were to be allowed to do this without the knowledge and consent of the end-user, it would be considered a serious breach of security for the browser.

On the other hand, you can write an ActiveX Object which will be able to get the default printer name and pass that to the server via a postback.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now