Click here to Skip to main content
15,885,683 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
When printing the receipt through the reportviewer memory leakage is arising. Even though I have tried other solutions mentioned non of it didn't give me any positive outcome. I have run the garbage collector but still did not get any outcome properly. Please give me a proper solution to sort out the matter.


What I have tried:

static string myconn = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;
SqlConnection conn = new SqlConnection(myconn);
readonly SalesF sf;
public RecieptForm(SalesF frm)
{
    InitializeComponent();
    this.sf = frm;

}

private void RecieptForm_Load(object sender, EventArgs e)
{
    this.reportViewer1.RefreshReport();
}
public void LoadReceipt(string cashTendered, string cashBalance)
{
    ReportDataSource receiptds;
    try
    {
        this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + @"\Reports\CashPaymentReciept.rdlc";
        this.reportViewer1.LocalReport.DataSources.Clear();
        DataSet1 dc = new DataSet1();
        SqlDataAdapter sda = new SqlDataAdapter();
        string query = "select c.id,c.transno,c.productid,c.price,c.quantity,c.total,c.transactiondate,p.description from tbl_cart as c inner join product as p on p.productID=c.productid where transno like '" + sf.lblInvoiceNumber.Text + "'";
        conn.Open();
        sda.SelectCommand = new SqlCommand(query, conn);
        sda.Fill(dc.Tables["dtSold"]);

        sda.Dispose();
        dc.Dispose();

        ReportParameter cartTotal = new ReportParameter("cartTotal", sf.lblAmount.Text);
        ReportParameter cashTend = new ReportParameter("cashTendered", cashTendered);
        ReportParameter cashBal = new ReportParameter("cashBalance", cashBalance);
        ReportParameter storeName = new ReportParameter("storeName", store.ToUpper());
        ReportParameter cashier = new ReportParameter("cashier", "Cashier: " + sf.lblName.Text);



        reportViewer1.LocalReport.SetParameters(cartTotal);
        reportViewer1.LocalReport.SetParameters(cashTend);
        reportViewer1.LocalReport.SetParameters(cashBal);
        reportViewer1.LocalReport.SetParameters(storeName);
        reportViewer1.LocalReport.SetParameters(cashier);
        receiptds = new ReportDataSource("DataSet1", dc.Tables["dtSold"]);
        reportViewer1.LocalReport.DataSources.Add(receiptds);

        reportViewer1.LocalReport.PrintToPrinter();



    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        sf.Refresh();
        reportViewer1.Dispose();
        GC.SuppressFinalize(reportViewer1);
        conn.Close();
        conn.Dispose();

    }
}
Posted
Updated 14-Jun-23 12:00pm
Comments
ZurdoDev 29-Jun-20 15:21pm    
Why do you think there is a memory leak? And what version of report viewer control is it?
Luc Pattyn 29-Jun-20 17:31pm    
1. I don't understand your code. There is a rather early
dc.Dispose();
and a dozen lines below that I see
receiptds = new ReportDataSource("DataSet1", dc.Tables["dtSold"]);


2. I strongly recommend you apply nested using statements for everything disposable (conn, cmd, ds, whatever) so everything gets scoped properly and gets disposed of automatically at the right moment.

:)

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