Link to home
Start Free TrialLog in
Avatar of Mani Pazhana
Mani PazhanaFlag for United States of America

asked on

Printing in C#

Hello Experts,

I am using this code:

-------------------------------------

public bool PrintReport(string ReportPath)
            {
                  this.RenderedReport = this.RenderReport(ReportPath);
                  try
                  {
                        if (m_numberOfPages < 1)
                        {
                              return false;
                        }
                        PrinterSettings printerSettings = new PrinterSettings();
                        string pkInstalledPrinters;

                        //PageSettings objprinter = new PageSettings();
                        //object strname = objprinter.PrinterSettings.PrinterName;

                        pkInstalledPrinters = printerSettings.PrinterName;                        
                        printerSettings.PrinterName =       pkInstalledPrinters;       
                        

                        printerSettings.MaximumPage = m_numberOfPages;
                        printerSettings.MinimumPage = 1;
                        printerSettings.PrintRange = PrintRange.SomePages;
                        printerSettings.FromPage = 1;
                        printerSettings.ToPage = m_numberOfPages;
                        
                        PrintDocument pd = new PrintDocument();
                        printerSettings.PrinterName = pd.PrinterSettings.PrinterName;

                        m_currentPrintingPage = 1;
                        m_lastPrintingPage = m_numberOfPages;
                        pd.PrinterSettings = printerSettings;
                        Console.WriteLine("Printing report...");
                        pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);

                        pd.Print();
                  }
                  catch (Exception ex)
                  {
                        Console.WriteLine(ex.Message);
                  }
                  finally
                  {
                  }
                  return true;
            }


------------------------------------

I am getting this message when i debug:

printerSettings.PrinterName =       "<no default printer>"      string

any idea?  I have a printer with default on my local machine.

Thanks

Avatar of MyersA
MyersA

Hi mani_sai,

If you add a breakpoint to the line:
pkInstalledPrinters = printerSettings.PrinterName;                    

and check its value of printerSettings.PrinterName at that point, do you get a valid name?
Also, did you double-check that in the Windows 'Printers and Faxes' menu you have a printer marked with the black checkmark?

Vaughn
Avatar of Mani Pazhana

ASKER

Vaughn-Thanks for your reply.

I set a break point and got this value:

printerSettings.PrinterName =      "<no default printer>"     string

also i checked in Windows 'Printers and Faxes'

i have a printer marked with black checkmark.

any idea?

The same code was working before, suddenly it is not getting default printer name.

I reinstalled the network printer and made it default, still the same issue.

any idea?

Thanks
Avatar of Bob Learned
What does printerSettings.InstalledPrinters.Count return?

Bob
printerSettings.InstalledPrinters.Count()

returns 0.

also if i hard code the printer name, it is working fine.

printerSettings.PrinterName = "Xerox DocuPrint N40";


ASKER CERTIFIED SOLUTION
Avatar of MyersA
MyersA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your link, it is security issue with IIS.