Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want the Print button excute print

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;

namespace MK_POS
{
    public partial class frmReceipt : Form
    {
        SqlConnection cn = new SqlConnection();
        SqlCommand cm = new SqlCommand();
        SqlDataReader dr;
        DBConnection dbcon = new DBConnection();

        frmPOS f;

        public frmReceipt(frmPOS frm)
        {
            InitializeComponent();
            cn = new SqlConnection(dbcon.MyConnection());
            f = frm;
            this.KeyPreview = true;
            getStoreInfo();
        }

        private void frmReceipt_Load(object sender, EventArgs e)
        {

            this.reportViewer1.RefreshReport();
        }
        public void LoadReport(string pcash, string pchange)
        {
            ReportDataSource rptDataSource;
            try
            {
                this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + @"\Reports\Receipt.rdlc";
                this.reportViewer1.LocalReport.DataSources.Clear();

                DataSet1 ds = new DataSet1();
                SqlDataAdapter da = new SqlDataAdapter();

                cn.Open();
                da.SelectCommand = new SqlCommand("select c.id, c.transno, c.pcode, c.price, c.qty, c.disc, c.total, c.sdate, c.status, p.pdesc from tblcart as c inner join tblproduct as p on p.pcode = c.pcode where transno like '" + f.lblTransno.Text + "'", cn);
                da.Fill(ds.Tables["dtSold"]);
                cn.Close();

                ReportParameter pVatable = new ReportParameter("pVatable", f.lblVatable.Text);
                ReportParameter pVat = new ReportParameter("pVat", f.lblVat.Text);
                ReportParameter pDiscount = new ReportParameter("pDiscount", f.lblDiscount.Text);
                ReportParameter pTotal = new ReportParameter("pTotal", f.lblTotal.Text);
                ReportParameter pCash = new ReportParameter("pCash", pcash);
                ReportParameter pChange = new ReportParameter("pChange", pchange);
                ReportParameter pTransaction = new ReportParameter("pTransaction", "Invoice #: " + f.lblTransno.Text);
                ReportParameter pCashier = new ReportParameter("pCashier", f.lblUser.Text);

                ReportParameter pHeader = new ReportParameter("pHeader", lblStore.Text);
                ReportParameter pAddress = new ReportParameter("pAddress", lblAddress.Text);
                ReportParameter pEmail = new ReportParameter("pEmail", "Email: " + lblEmail.Text);
                ReportParameter pPhone = new ReportParameter("pPhone", "Phone: " + lblPhone.Text);
                ReportParameter dVat = new ReportParameter("dVat", "TIN No.: " + lblVat.Text);

                reportViewer1.LocalReport.SetParameters(pHeader);
                reportViewer1.LocalReport.SetParameters(pAddress);
                reportViewer1.LocalReport.SetParameters(pEmail);
                reportViewer1.LocalReport.SetParameters(pPhone);
                reportViewer1.LocalReport.SetParameters(dVat);

                reportViewer1.LocalReport.SetParameters(pVatable);
                reportViewer1.LocalReport.SetParameters(pVat);
                reportViewer1.LocalReport.SetParameters(pDiscount);
                reportViewer1.LocalReport.SetParameters(pTotal);
                reportViewer1.LocalReport.SetParameters(pCash);
                reportViewer1.LocalReport.SetParameters(pChange);
                reportViewer1.LocalReport.SetParameters(pTransaction);
                reportViewer1.LocalReport.SetParameters(pCashier);

                rptDataSource = new ReportDataSource("DataSet1", ds.Tables["dtSold"]);
                reportViewer1.LocalReport.DataSources.Add(rptDataSource);
                reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
                reportViewer1.ZoomMode = ZoomMode.Percent;
                reportViewer1.ZoomPercent = 100;
            }
            catch (Exception ex)
            {
                cn.Close();
                MessageBox.Show(ex.Message);
            }

        }
        public void getStoreInfo()
        {
            cn.Open();
            cm = new SqlCommand("select * from tblStore", cn);
            dr = cm.ExecuteReader();
            dr.Read();
            if (dr.HasRows)
            {
                lblStore.Text = dr["store"].ToString();
                lblAddress.Text = dr["address"].ToString();
                lblEmail.Text = dr["email"].ToString();
                lblPhone.Text = dr["phone"].ToString();
                lblVat.Text = dr["tin"].ToString();
            }
            dr.Close();
            cn.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void frmReceipt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                btnClose_Click(sender, e);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 2-Jul-23 23:04pm
Comments
Richard Deeming 5-Jul-23 4:36am    
da.SelectCommand = new SqlCommand("select c.id, c.transno, c.pcode, c.price, c.qty, c.disc, c.total, c.sdate, c.status, p.pdesc from tblcart as c inner join tblproduct as p on p.pcode = c.pcode where transno like '" + f.lblTransno.Text + "'", cn);

Your code is almost certainly vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

da.SelectCommand = new SqlCommand("select c.id, c.transno, c.pcode, c.price, c.qty, c.disc, c.total, c.sdate, c.status, p.pdesc from tblcart as c inner join tblproduct as p on p.pcode = c.pcode where transno like @transno", cn);
da.SelectCommand.Parameters.AddWithValue("@transno", f.lblTransno.Text);

Google Search is a good place to start. I used a slightly modified version of your question: c# how to execute print button to print reciept on RDLC with reportviewer[^] and found many answers, like:
* C# Tutorial - How to Print RDLC Report without Report Viewer | FoxLearn - YouTube[^] (yes, without Report Viewer, but shared anyway, #1 answer)
* How to Print Receipt in ASP.NET Core using RDLC Report - YouTube[^]

And there are many more. Choose the solution that best suits your needs. NOTE: Specify the app type for more targeted answers, like: WinForm, Wpf, Asp.net, etc...
 
Share this answer
 
You can use the 'PrintDialog' method as explained in ReportViewer.PrintDialog Method ()[^], it does however depends on the version of the ReportViewer control you are using -

private void btnPrint_Click(object sender, EventArgs e)
{
    try
    {
        // Print the report directly
        reportViewer1.PrintDialog();
    }
    catch (Exception ex)
    {
        MessageBox.Show("An error occurred while printing my receipt: " + ex.Message);
    }
}


This will open the print dialog that will allow the user to print or cancel the job.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900