locked
how to Print on client side in asp.net c# RRS feed

  • Question

  • User-367318540 posted

    i want to print on client side ,currently below mentioned code working on server ,where i published my application,when i am giving print from client side ,then print is coming on server printer but i want print on client printer not on server.

    protected void btnprnt_Click(object sender, EventArgs e)
            {
                if (Convert.ToInt32(txtqty.Text) > 0)
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("sp_Importlabelprint", con);
                        cmd.CommandType = CommandType.StoredProcedure;
    
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        //SqlDataAdapter adp = new SqlDataAdapter("select prdno,orderno,Codeitem,prdqty from probale", con);
                        //DataSet ds = new DataSet();
                        //adp.Fill(ds);
    
                        for (int i = 0; i < Convert.ToInt32(txtqty.Text); i++)
                        {
                            ReportDocument rpt = new ReportDocument();
                            rpt.FileName = Server.MapPath("~/Importlbprint.rpt");
                            rpt.SetDataSource(ds.Tables["Table"]); //DS is Dataset which you want to print
    
                            rpt.SetParameterValue("PageNo", i + 1);
                            rpt.SetParameterValue("TotalPage", Convert.ToInt32(txtqty.Text));
                            rpt.PrintToPrinter(1, false, 1, 1);
                        }
                        string url = "/lbimportprints.aspx?noc=" + txtqty.Text;
                        Page.ClientScript.RegisterStartupScript(
                     this.GetType(), "OpenWindow", "window.open('" + url + "','_newtab');", true);
    
                    }
    
                    catch (Exception ex) { }
    
                }
            }
        }

    Tuesday, September 17, 2019 9:10 AM

All replies

  • User475983607 posted

    akhterr

    i want to print on client side ,currently below mentioned code working on server ,where i published my application,when i am giving print from client side ,then print is coming on server printer but i want print on client printer not on server.

    The shared code runs on the server and why it prints from the server.   Aside from the many code issues shown, which you should debug and fix, you should return the content to the browser where the user can click file -> print.

    Tuesday, September 17, 2019 11:19 AM
  • User-367318540 posted

    dear mgebhard,

    debug is not enough ,you are requested to guide me.

    Tuesday, September 17, 2019 12:29 PM
  • User753101303 posted

    Hi,

    The simplest option is to have the user using the "print menu" found in his web browser to print the web page shown in the browser (you can control styles which are applied when printing) or to have them print a PDF file.

    For now the problem is that you try to do that from C# code which runs server side and doesn't have access to client side printers...

    Edit: and the "not simplest" option is to use tools or scripts claiming to print without any user action but I never tried them (AFAIK it is not supposed to happen for safety reason, I always favored having the user printing a web page or a PDF file).

    Tuesday, September 17, 2019 1:13 PM
  • User-367318540 posted

    Hi PatriceSc,

    i am giving multiple print of one report ,on brower it is getting display one page,that why i want directly print without previewing .

    For now the problem is that you try to do that from C# code which runs server side and doesn't have access to client side printers...

    how to run c# code on client side,above you told?

    Tuesday, September 17, 2019 1:19 PM
  • User475983607 posted

    how to run c# code on client side,above you told?

    It is not possible because C# code runs on a server not the client. 

    The user must print the document.  A web server cannot cause the browser to print a document.  If that were possible then spanners/hackers could print ads on client printers.

    Tuesday, September 17, 2019 1:25 PM
  • User753101303 posted

    You can run C# code client side under very special circumstances (using blazor/"web assembly") and it won't necessarily give you access to client side printers.

    What I see sometimes is suggestions such as https://stackoverflow.com/questions/4292373/javascript-print-without-print-dialog-box but I never tried them yet myself. I'll perhaps give this a quick try but AFAIK it depends on the browser or can be enabled automatically if you have control on machines (ie a POS like application inside your company rarher than something for the general public).

    Tuesday, September 17, 2019 1:31 PM
  • User-367318540 posted

    Hi mgebhard,

    so how could i give multiple print of one report using viewer,as currently it is display me one page display,for example i want to print 5 times of one report ,then it get open in browser in a PDF but have five pages.

    Tuesday, September 17, 2019 1:33 PM
  • User475983607 posted

    akhterr

    so how could i give multiple print of one report using viewer,as currently it is display me one page display,for example i want to print 5 times of one report ,then it get open in browser in a PDF but have five pages.

    Do you mean the PDF should have 5 pages?  If so, then you have a bug in the report generation.  I assume the logic is within the /lbimportprints.aspx page.

    If you want to print 5 copies of the report, this can be done in the print preview screen.

    Tuesday, September 17, 2019 1:40 PM