这可能很难解释。我有一个程序可以填写基于 Oracle 数据库的预建 Crystal Report (CR 2011)。我已经通过基于 MSDN 教程的代码将登录信息应用于报告中的所有表格(以及 5 个子报告中的所有表格)。如果我在报告刷新后不添加任何代码对报告进行任何操作(例如保存或发送到打印机),它将毫无问题地运行我的代码。但是,如果我添加任何使程序有用的代码,它会因以下异常而中断。
这是我的相关代码:
try
{
for (Intcount = 1; Intcount < rName.Count(); Intcount++)
{
LogFile.LogMessage(path, sReportPath);
LogFile.LogMessage(path, sReportPath + rName.ElementAt(Intcount));
//This line is loading "C:\...\Policy Reinstatement.rpt" which is valid
cr.Load(sReportPath + rName.ElementAt(Intcount));
crConnectionInfo.ServerName = sDataSource;
crConnectionInfo.DatabaseName = "";
crConnectionInfo.UserID = sUserID;
crConnectionInfo.Password = sPassword;
cr.SetDatabaseLogon(crConnectionInfo.UserID, crConnectionInfo.Password,
crConnectionInfo.ServerName, crConnectionInfo.DatabaseName);
//Method for looping through each table in the report
//and applying the Connection Info to each of them
SetDBLogonForReport(crConnectionInfo, cr);
//Method for looping through all SubReports,
//then calling the SetDBLogonForReport method.
SetDBLogonForSubReports(crConnectionInfo, cr);
CrystalReportViewer rpt = new CrystalReportViewer();
rpt.ReportSource = cr;
try
{
rpt.Refresh();
}
catch (Exception ex)
{
//It does not throw on this exception
LogFile.LogError(path, ex);
throw;
}
cr.PrintToPrinter(1, false, 0, 1);
}
}
catch (Exception ex)
{
//It throws on this exception
LogFile.LogError(path, ex);
throw;
}
这是一个例外:
Message: Failed to open the connection.Policy Reinstatement {35A94093-A135-462DA5AA-F5FDD3E2D62B}.rpt
Source: CrystalDecisions.ReportAppServer.DataSetConversion
StackTrace: at CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e)
at CrystalDecisions.ReportSource.EromReportSourceBase.HandleException(Exception exception)
at CrystalDecisions.ReportSource.EromReportSourceBase.GetLastPageNumber(ReportPageRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.PrintToPrinter(Int32 nCopies, Boolean collated, Int32 startPageN, Int32 endPageN)
at CrystalDecisions.CrystalReports.Engine.ReportDocument.PrintToPrinter(Int32 nCopies, Boolean collated, Int32 startPageN, Int32 endPageN)
Inner Message:
Failed to open the connection.
Policy Reinstatement {35A94093-A135-462D-A5AA-F5FDD3E2D62B}.rpt
Inner Source: rptcontrollers.dll
Inner StackTrace: at CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.GetLastPageNumber(RequestContext pRequestContext)
at CrystalDecisions.ReportSource.EromReportSourceBase.GetLastPageNumber(ReportPageRequestContext reqContext)
在此先感谢您的帮助。