使用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();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值