In PageInit():
CrystalDecisions.Shared.ConnectionInfo connectionInfo = new CrystalDecisions.Shared.ConnectionInfo();
connectionInfo.ServerName = qServer;
connectionInfo.UserID = userID;
connectionInfo.Password = password;
// set report connection for main report
SetDBLogonForReport(connectionInfo, crReport, qServer);
// set report connection for any subreports
SetDBLogonForSubreports(connectionInfo, crReport, qServer);
// view report
crystalReportViewer.ReportSource = crReport;
-----------
private void SetDBLogonForReport(
CrystalDecisions.Shared.ConnectionInfo connectionInfo,
ReportDocument reportDocument,
string qServer)
{
Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
-------------
private void SetDBLogonForSubreports(
CrystalDecisions.Shared.ConnectionInfo connectionInfo,
ReportDocument reportDocument,
string qServer)
{
Sections sections = reportDocument.ReportDefinition.Sections;
foreach (Section section in sections)
{
ReportObjects reportObjects = section.ReportObjects;
foreach (ReportObject reportObject in reportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subreportObject = (SubreportObject)reportObject;
ReportDocument subReportDocument =
subreportObject.OpenSubreport(subreportObject.SubreportName);
SetDBLogonForReport(connectionInfo, subReportDocument, qServer);
}
}
}
}