locked
IIS can access local printers RRS feed

  • Question

  • User-1379762086 posted

    I built a web application that printer text to the default printer on my local machine, the application run well when using visual studio but when i deployed it to IIS of the same system i got an exception "No printers are installed". The application uses document.print to send the text to the local printer. Please i will appreciate your help

    Tuesday, June 30, 2015 3:24 PM

Answers

  • User-1379762086 posted

    Thank you everybody that contributed to this post i have found the solution to this problem, if you are have this same problem you can read these two article below.

    https://msdn.microsoft.com/en-us/library/aa290045(v=vs.71).aspx

    http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.dwnet_2.0.dwprgnet/html/dwprgnet/BEICAAJF.htm

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 2, 2015 5:14 PM

All replies

  • User-821857111 posted

    ASP.NET web applications execute on web servers. If you have ASP.NET code that communicates directly with a printer, it can only do so with printers that are connected to the machine that the code is executing on - i.e. the web server. You cannot print directly to a printer connected to a machine in a different network.

    Wednesday, July 1, 2015 3:17 AM
  • User-1379762086 posted

    Thank you Mikesdotnett for your reply, but like i told you the printer is connected in same machine where the application is installed and am also running it in same machine ie my localhost.

    Wednesday, July 1, 2015 4:47 AM
  • User-821857111 posted

    Can you paste the actual code that you are using to print, and explain how the No Printers Installed error message is presented to you?

    Wednesday, July 1, 2015 5:15 AM
  • User-1379762086 posted

    The part of the code that throws the exception is Document.Print  and the message is just "No Printers Installed". But the application runs well when run from visual studio.

    Wednesday, July 1, 2015 7:02 AM
  • User-821857111 posted

    So you can't paste the code I asked for, or describe how the error message is presented?

    Wednesday, July 1, 2015 8:20 AM
  • User753101303 posted

    Hi,

    Printers are configured for a particular user. When running VS you can use printers configured for your account. When using IIS on the same machine, it uses another account that doesn't have any printer installed. If I remember you can configure default printers for this account or another option might be to use a network printer (even if on the same machine) so that you don't have this kind of configuration.

    Note also of course that it will print on "your" printer here but that for a real web server it would print on the configured server side and won't print on a client side configured printer.

    Wednesday, July 1, 2015 8:27 AM
  • User-1379762086 posted

    Sorry Mikesdotnett for the delay, i really appreciate your effort. Bellow is the code

    private System.Windows.Forms.Label lblInfo;
    private System.Drawing.Printing.PrintDocument printDoc;
    private System.Windows.Forms.ComboBox cboLocalPrinters;
    private System.Windows.Forms.Button btnPrint;
    private System.Windows.Forms.ComboBox cboNetworkPrinters;
    private System.Windows.Forms.RadioButton radioLocalPrinters;
    private System.Windows.Forms.RadioButton radioNetworkPrinters;
    private System.Windows.Forms.Button btnExit;
    private System.Windows.Forms.Button btnRefresh;

    InitializeComponent();

    printDoc.Print();// this is the statement that returns the exception

    private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    IFormatProvider enGBDateFormat = new CultureInfo("en-GB").DateTimeFormat;
    int countN = Convert.ToInt32(lblSaleCount.Text);
    string earnedPts= lblPointbala.Text;
    string earnedpointvalue = lblAmountbala.Text;
    //We are simply going to build a string saying "This is a test".
    using (Graphics g = e.Graphics)
    {
    // Let's create a StringFormat object with each line of text, and the block of text centered on the page.
    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    stringFormat.LineAlignment = StringAlignment.Center;

    e.Graphics.DrawString("SPALOON SALOON", new Font("MS Mincho", 12, FontStyle.Bold), Brushes.Black, new Point(5, 0));
    e.Graphics.DrawString("Plot 8 house 2, Furo Ezimora street", new Font("MS Mincho", 10), Brushes.Black, new Point(5, 20));
    e.Graphics.DrawString("lekki phase 1 Lagos", new Font("MS Mincho", 10), Brushes.Black, new Point(5, 35));
    e.Graphics.DrawString("Phone: 08099996832", new Font("MS Mincho", 10), Brushes.Black, new Point(5, 50));
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["orbitcon"].ConnectionString);
    con.Open();

    SqlCommand cmd = new SqlCommand("select top " + countN + " p.ProductName, s.Quantity, TranAmount from tbl_Sales s, tbl_Products p where p.ProductCode=s.ProductCode order by TranID desc", con);

    cmd.ExecuteNonQuery();
    SqlDataAdapter adap = new SqlDataAdapter(cmd);

    DataSet ds = new DataSet();
    adap.Fill(ds, "DataSet1");
    DataTable dt = new DataTable();
    dt = ds.Tables[0];
    int yposition = 70;
    decimal totalTrans = 0;
    foreach (DataRow datR in dt.Rows)
    {

    e.Graphics.DrawString(datR["Quantity"].ToString() + " " + datR["ProductName"].ToString() + ": " + Convert.ToDecimal(datR["TranAmount"]).ToString("#,##0.00"), new Font("MS Mincho", 8), Brushes.Black, new Point(5, yposition));
    yposition = yposition + 13;
    totalTrans = Convert.ToDecimal(datR["TranAmount"]) + totalTrans;
    }
    con.Close();
    e.Graphics.DrawString("Total: " + totalTrans.ToString("#,##0.00"), new Font("MS Mincho", 8, FontStyle.Bold), Brushes.Black, new Point(5, yposition + 7));
    string todayy = DateTime.Now.ToString();

    e.Graphics.DrawString("Purchase Date: " + Convert.ToDateTime(todayy, enGBDateFormat).ToString("dd/MM/yyyy"), new Font("MS Mincho", 8), Brushes.Black, new Point(5, yposition + 22));
    e.Graphics.DrawString("Point Earned So far: " + earnedPts.ToString(), new Font("MS Mincho", 8), Brushes.Black, new Point(5, yposition + 34));
    e.Graphics.DrawString("Point Value: " + Convert.ToDecimal(earnedpointvalue).ToString("#,##0.00"), new Font("MS Mincho", 8), Brushes.Black, new Point(5, yposition + 46));
    e.Graphics.DrawString("Thank you for your patronage,", new Font("MS Mincho", 10), Brushes.Black, new Point(5, yposition + 65));
    e.Graphics.DrawString("Please note that you can shop with", new Font("MS Mincho", 10), Brushes.Black, new Point(5, yposition + 80));
    e.Graphics.DrawString("your point if you have earned", new Font("MS Mincho", 10), Brushes.Black, new Point(5, yposition + 95));
    e.Graphics.DrawString("enough point.", new Font("MS Mincho", 10), Brushes.Black, new Point(5, yposition + 110));
    }
    }

    rivate System.Windows.Forms.Label lblInfo;
    private System.Drawing.Printing.PrintDocument printDoc;
    private System.Windows.Forms.ComboBox cboLocalPrinters;
    private System.Windows.Forms.Button btnPrint;
    private System.Windows.Forms.ComboBox cboNetworkPrinters;
    private System.Windows.Forms.RadioButton radioLocalPrinters;
    private System.Windows.Forms.RadioButton radioNetworkPrinters;
    private System.Windows.Forms.Button btnExit;
    private System.Windows.Forms.Button btnRefresh;

    private void InitializeComponent()
    {
    this.lblInfo = new System.Windows.Forms.Label();
    this.printDoc = new System.Drawing.Printing.PrintDocument();
    this.cboLocalPrinters = new System.Windows.Forms.ComboBox();
    this.btnPrint = new System.Windows.Forms.Button();
    this.cboNetworkPrinters = new System.Windows.Forms.ComboBox();
    this.radioLocalPrinters = new System.Windows.Forms.RadioButton();
    this.radioNetworkPrinters = new System.Windows.Forms.RadioButton();
    this.btnExit = new System.Windows.Forms.Button();
    this.btnRefresh = new System.Windows.Forms.Button();
    // this.SuspendLayout();
    //
    // lblInfo
    //
    this.lblInfo.AutoSize = true;
    this.lblInfo.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.lblInfo.Location = new System.Drawing.Point(12, 9);
    this.lblInfo.Name = "lblInfo";
    this.lblInfo.Size = new System.Drawing.Size(221, 13);
    this.lblInfo.TabIndex = 0;
    this.lblInfo.Text = "I want to print to this specific printer: ";
    //
    // printDoc
    //
    this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);
    //
    // cboLocalPrinters
    //
    this.cboLocalPrinters.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.cboLocalPrinters.FormattingEnabled = true;
    this.cboLocalPrinters.Location = new System.Drawing.Point(15, 59);
    this.cboLocalPrinters.Name = "cboLocalPrinters";
    this.cboLocalPrinters.Size = new System.Drawing.Size(255, 21);
    this.cboLocalPrinters.TabIndex = 1;
    //
    // btnPrint
    //
    this.btnPrint.Location = new System.Drawing.Point(195, 175);
    this.btnPrint.Name = "btnPrint";
    this.btnPrint.Size = new System.Drawing.Size(75, 23);
    this.btnPrint.TabIndex = 2;
    this.btnPrint.Text = "Print";
    this.btnPrint.UseVisualStyleBackColor = true;
    // this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
    //
    // cboNetworkPrinters
    //
    this.cboNetworkPrinters.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.cboNetworkPrinters.FormattingEnabled = true;
    this.cboNetworkPrinters.Location = new System.Drawing.Point(15, 115);
    this.cboNetworkPrinters.Name = "cboNetworkPrinters";
    this.cboNetworkPrinters.Size = new System.Drawing.Size(255, 21);
    this.cboNetworkPrinters.TabIndex = 5;
    //
    // radioLocalPrinters
    //
    this.radioLocalPrinters.AutoSize = true;
    this.radioLocalPrinters.Location = new System.Drawing.Point(15, 36);
    this.radioLocalPrinters.Name = "radioLocalPrinters";
    this.radioLocalPrinters.Size = new System.Drawing.Size(88, 17);
    this.radioLocalPrinters.TabIndex = 6;
    this.radioLocalPrinters.TabStop = true;
    this.radioLocalPrinters.Text = "Local printers";
    this.radioLocalPrinters.UseVisualStyleBackColor = true;
    //this.radioLocalPrinters.CheckedChanged += new System.EventHandler(this.radioLocalPrinters_CheckedChanged);
    //
    // radioNetworkPrinters
    //
    this.radioNetworkPrinters.AutoSize = true;
    this.radioNetworkPrinters.Location = new System.Drawing.Point(15, 92);
    this.radioNetworkPrinters.Name = "radioNetworkPrinters";
    this.radioNetworkPrinters.Size = new System.Drawing.Size(102, 17);
    this.radioNetworkPrinters.TabIndex = 7;
    this.radioNetworkPrinters.TabStop = true;
    this.radioNetworkPrinters.Text = "Network printers";
    this.radioNetworkPrinters.UseVisualStyleBackColor = true;
    // this.radioNetworkPrinters.CheckedChanged += new System.EventHandler(this.radioNetworkPrinters_CheckedChanged);
    //
    // btnExit
    //
    this.btnExit.Location = new System.Drawing.Point(33, 175);
    this.btnExit.Name = "btnExit";
    this.btnExit.Size = new System.Drawing.Size(75, 23);
    this.btnExit.TabIndex = 8;
    this.btnExit.Text = "Exit";
    this.btnExit.UseVisualStyleBackColor = true;
    // this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
    //
    // btnRefresh
    //
    this.btnRefresh.Location = new System.Drawing.Point(114, 175);
    this.btnRefresh.Name = "btnRefresh";
    this.btnRefresh.Size = new System.Drawing.Size(75, 23);
    this.btnRefresh.TabIndex = 9;
    this.btnRefresh.Text = "Refresh";
    this.btnRefresh.UseVisualStyleBackColor = true;
    // this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
    //
    // FormPrint
    //

    }

    Wednesday, July 1, 2015 10:45 AM
  • User-821857111 posted

    I thought so. That's a Windows Forms application, not a web application. Your question is off topic here.

    Wednesday, July 1, 2015 5:32 PM
  • User-1379762086 posted

    Yes i used some windows form objects but the application is a web application

    Wednesday, July 1, 2015 7:08 PM
  • User-1379762086 posted

    Thank you everybody that contributed to this post i have found the solution to this problem, if you are have this same problem you can read these two article below.

    https://msdn.microsoft.com/en-us/library/aa290045(v=vs.71).aspx

    http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.dwnet_2.0.dwprgnet/html/dwprgnet/BEICAAJF.htm

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 2, 2015 5:14 PM