使用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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章