Утечка памяти Reportviewer C#
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.
Что я уже пробовал:
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(); } }
ZurdoDev
Как вы думаете, почему произошла утечка памяти? И что это за версия средства просмотра отчетов?
Luc Pattyn
1. Я не понимаю ваш код. Есть довольно рано
dc.Dispose();и дюжина строк ниже этого я вижу
receiptds = new ReportDataSource("DataSet1", dc.Tables["dtSold"]);
2. я настоятельно рекомендую вам применять вложенные операторы using для всего одноразового (conn, cmd, ds, что угодно), чтобы все было правильно определено и автоматически удалялось в нужный момент.
:)