使用CrystalReportViewer打印通訊錯誤的問題

今天在使用CrystalReportViewer打印Report的時候,總是會跳出:An communication error occurred. Printing will be stopped的錯誤。在網上搜了幾次,始終沒有找到正確的答案。
後來只好自己debug找答案了。
後來我發現,在點擊“打印”按鈕時,會使頁面發生postback,從而使report重新加載,我個人認爲就在問題就在這裏,CrystalReportViewer在點擊打印時,頁面會自動進行回傳,然後再服務器端重新加載Report的內容,同時彈出打印的對話框,但是在這個過程中,,CrystalReportViewer會重新加載,也就是會重新連接數據庫。所以你要在postBack過程中再給你的CrystalReportViewer設置一下數據庫的連接字符串。
所以通常解決這個的辦法是在page_load事件中綁定CrystalReportViewer的數據源,當然你也可以在CrystalReportViewer的initial事件中進行綁定,但是由於這是CrystalReportViewer還沒有加載,此時你不能顯示的使用CrystalReportViewer進行綁定。總結一下這兩種方法:
1.在page_load事件中綁定:
(a) CrystalReportViewer的autoDataBind屬性設爲false
cryptSource.ReportDocument.FileName = “C:/report.rpt”;
cryptSource.ReportDocument.SetDatabaseLogon(
"sa""sa""demoServer""test");
cryptSource.ReportDocument.VerifyDatabase();
cryptSource.DataBind();
CrystalReportViewer1.DataBind();
(b) CrystalReportViewer的autoDataBind屬性設爲true
cryptSource.ReportDocument.FileName = “C: eport.rpt”;
cryptSource.ReportDocument.SetDatabaseLogon(
"sa""sa""demoServer""test");
cryptSource.ReportDocument.VerifyDatabase();
cryptSource.DataBind();
2. 在CrystalReportViewer的Init事件中綁定
(a) CrystalReportViewer的autoDataBind屬性設爲false
    這種情況在上面已經提過,由於CrystalReportViewer控件還沒有加載,所以不能顯示綁定。
(b) CrystalReportViewer的autoDataBind屬性設爲true
cryptSource.ReportDocument.FileName = “C: eport.rpt”;
cryptSource.ReportDocument.SetDatabaseLogon(
"sa""sa""demoServer""test");
cryptSource.ReportDocument.VerifyDatabase();
cryptSource.DataBind();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章