4

I want to call a specific printer to print in my WPF application. I have three printer Suppose Printer1 for Bar bill Print Printer2 for Kitchen bill Print Printer3 for Guest bill Print printers name already saved in database, while printing I get a printer name from DB and want to print from specific printer, not defaul printer Here is my code

var v = new PrinterDAL().GetPrinterSettings();
try
{
   System.Threading.Thread thread = new System.Threading.Thread(new 
      System.Threading.ThreadStart(
            delegate()
            {   
                gridPrint.Dispatcher.Invoke(DispatcherPriority.Normal,
                    new Action(
                    delegate()
                    {
                            PrintDialog printDialog = new PrintDialog();
                            printDialog.PrintQueue = new PrintQueue(
                                new PrintServer(@"\\" + v.BarPrinter), "");
                            printDialog.PrintVisual(gridPrint, "");
                            this.Close();
                    }
                ));
            }
            ));
            thread.Start();
}
catch (Exception ex)
{
     Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "", MessageBoxButton.OK, 
                                       MessageBoxImage.Error);
}

I get an exception from this code

"An exception occurred while creating the PrintServer object. Win32 error: The printer name is invalid."

5
  • Given that we're not mind readers - what does v.BarPrinter contain? Commented Nov 13, 2012 at 7:38
  • It contains the name of the printer, which is the error here. Can you print out the name and check why .NET cannot communicate with the printer? Commented Nov 13, 2012 at 7:57
  • If the answer is correct. Please check it as accepted. Other people can find the answer in a faster way. Commented Nov 13, 2012 at 9:49
  • i have marked as correct Commented Nov 13, 2012 at 9:52
  • Doesn't appear as correct in the system. Just a +1 in usefulness. Accept it as best answer... if it looks like the best answer for you, of course. Commented Nov 13, 2012 at 10:00

1 Answer 1

9

PrintServer must be instance using a computer or printer server device in UNC format (\\resource) not a printer name:

For example, if the name of your computer, in your domain, is KashifPC and you have configured a printer, called "Printer1", you can use:

//example code. no error handling.
PrintServer localPS = New PrintServer(@"\\KashifPC")
PrinterQueue printer1 = localPS.GetPrintQueue("Printer1") //v.BarPrinter???
PrintDialog printDialog = new PrintDialog();
printDialog.PrintQueue = printer1
//rest of code
Sign up to request clarification or add additional context in comments.

1 Comment

jlvaquero you are brilliant..bundle of thanks.it has resolved my problem.i am facing this problem from last three days.........Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.