When running an app that shows a crystal report that has subreport, the user is prompted for login information.
Below is my code I am using to pass the login information to the report. This code is executed when the form is shown. I have a test vs2017 app that contains a form with a crystal report viewer on it. The login information is passed and works fine until I add a subreport. Once a subreport is added to my existing report the user is prompted to log into the database. Upon entering the correct login credentials, a login failed message appears. The report cannot be run. The report gets data from a command query from the database.
Sections crSections;
ReportDocument crReportDocument, crSubreportDocument;
SubreportObject crSubreportObject;
ReportObjects crReportObjects;
ConnectionInfo crConnectionInfo;
Database crDatabase;
Tables crTables;
TableLogOnInfo crTableLogOnInfo;
crReportDocument = new ReportDocument();
crReportDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, CrystalReport3.rpt"));
crDatabase = crReportDocument.Database;
crTables = crDatabase.Tables;
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = "myservname";
crConnectionInfo.DatabaseName = "mydatabasename";
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "myusername";
foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)
{
crTableLogOnInfo = aTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
aTable.ApplyLogOnInfo(crTableLogOnInfo);
}
// THIS STUFF HERE IS FOR REPORTS HAVING SUBREPORTS
// set the sections object to the current report's section
crSections = crReportDocument.ReportDefinition.Sections;
// loop through all the sections to find all the report objects
foreach (Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
//loop through all the report objects in there to find all
subreports
foreach (ReportObject crReportObject in crReportObjects)
{
if (crReportObject.Kind == ReportObjectKind.SubreportObject)
{
crSubreportObject = (SubreportObject)crReportObject;
//open the subreport object and logon as for the general report
crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
crDatabase = crSubreportDocument.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table aaTable in crTables)
{
crTableLogOnInfo = aaTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
aaTable.ApplyLogOnInfo(crTableLogOnInfo);
}
}
}
}
crystalReportViewer1.ReportSource = crReportDocument;
crystalReportViewer1.Refresh();
I expect the report to be run without prompting the user as I am passing in login credentials.