2014-11-04 47 views
0

我有一種方法將報告呈現爲PDF,以便它可以附加到電子郵件中。代碼如下:ReportViewer渲染器不會重置爲新數據

也能正常工作的第一次,但如果用戶產生另一個報告和發送電子郵件,附加到電子郵件的報告是第一位的產生,而不是當前的。

例如,用戶打開軟件,創建採購訂單#3,並生成PDF並通過電子郵件正確發送。用戶然後創建一個新的採購訂單而不退出該軟件,#4。提交#4後,附在電子郵件中的PDF是從#3開始的,就像ReportViewer沒有清除以前的數據。

即使使用ReportViewer界面生成新報告,它也不會影響附加到電子郵件的內容。我試圖清除ReportViewer之間的渲染無濟於事。

我在這裏錯過了什麼?

回答

0

我可以通過復位ReportDataSource功能的每一次迭代來糾正問題:

 this.DataTable1TableAdapter.Fill(this.reportdataset.DataTable1, ponum); 
     DataTable dt = new DataTable(); 
     dt = DataTable1TableAdapter.GetData(ponum); 
     Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt); 


     reportViewer1.LocalReport.DataSources.Add(rprtDTSource); 

     Warning[] warnings; 
     string[] streamids; 
     string mimeType; 
     string encoding; 
     string filenameExtension; 
     string filename = "Purchase Requisition " + ponum.ToString() + ".pdf"; 

     byte[] bytes = reportViewer1.LocalReport.Render(
      "PDF", null, out mimeType, out encoding, out filenameExtension, 
      out streamids, out warnings); 
     try 
     { 
      using (FileStream fs = new FileStream(filename, FileMode.Create)) 
      { 
       fs.Write(bytes, 0, bytes.Length); 
       fs.Flush();    
      } 

     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

     return filename;