Here's the code that we use to set the table login:
If it's not already there, add
using CrystalDecisions.CrystalReports.Engine
In the constructor, do the following:
ReportDocument crReport = new ReportDocument();
crReport.Load(<filename>);
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = <server>;
connectionInfo.DatabaseName = <db name>;
connectionInfo.UserID = <userID>;
connectionInfo.Password = <password>;
// set data connections for main report
SetDBLogonForReport(connectionInfo, crReport);
// set report connection for any subreports
SetDBLogonForSubreports(connectionInfo, crReport);
// view report
crystalReportViewer.ReportSource = crReport;
-------------
private void SetDBLogonForReport(
CrystalDecisions.Shared.ConnectionInfo connectionInfo,
ReportDocument reportDocument)
{
Tables tables = reportDocument.Database.Tables;
foreach (Table table in tables)
{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
-----------
private void SetDBLogonForSubreport(
CrystalDecisions.Shared.ConnectionInfo connectionInfo,
ReportDocument reportDocument)
{
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);
}
}
}
}
If you need to set the parameters for a report, you can do it at any time after the report is loaded into the Report Document and before the ReportSource of the viewer is set.