天天看点

使用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

使用CrystalReportViewer打印通讯错误的问题

cryptSource.ReportDocument.FileName  =  “C:/report.rpt”;

使用CrystalReportViewer打印通讯错误的问题

cryptSource.ReportDocument.SetDatabaseLogon( " sa " ,  " sa " ,  " demoServer " ,  " test " );

使用CrystalReportViewer打印通讯错误的问题

cryptSource.ReportDocument.VerifyDatabase();

使用CrystalReportViewer打印通讯错误的问题

cryptSource.DataBind();

使用CrystalReportViewer打印通讯错误的问题

CrystalReportViewer1.DataBind(); (b) CrystalReportViewer的autoDataBind属性设为true

使用CrystalReportViewer打印通讯错误的问题

cryptSource.ReportDocument.FileName  =  “C: eport.rpt”;

使用CrystalReportViewer打印通讯错误的问题

cryptSource.ReportDocument.SetDatabaseLogon( " sa " ,  " sa " ,  " demoServer " ,  " test " );

使用CrystalReportViewer打印通讯错误的问题

cryptSource.ReportDocument.VerifyDatabase();

使用CrystalReportViewer打印通讯错误的问题

cryptSource.DataBind(); 2. 在CrystalReportViewer的Init事件中绑定

(a) CrystalReportViewer的autoDataBind属性设为false

    这种情况在上面已经提过,由于CrystalReportViewer控件还没有加载,所以不能显示绑定。

(b) CrystalReportViewer的autoDataBind属性设为true

使用CrystalReportViewer打印通讯错误的问题

cryptSource.ReportDocument.FileName  =  “C: eport.rpt”;

使用CrystalReportViewer打印通讯错误的问题

cryptSource.ReportDocument.SetDatabaseLogon( " sa " ,  " sa " ,  " demoServer " ,  " test " );

使用CrystalReportViewer打印通讯错误的问题

cryptSource.ReportDocument.VerifyDatabase();

使用CrystalReportViewer打印通讯错误的问题

cryptSource.DataBind();