Symptom
How to login to subreport using Crystal Report .Net SDK?
Environment
- Crystal Reports 2008 Crystal Reports for Visual Studio 2010
Resolution
- Here is the C# code snippet to login in subreport using Crystal Report .Net SDK.
Sections crSections;
ReportDocument crReportDocument, crSubreportDocument;
SubreportObject crSubreportObject;
ReportObjects crReportObjects;
ConnectionInfo crConnectionInfo;
Database crDatabase;
Tables crTables;
TableLogOnInfo crTableLogOnInfo;
crReportDocument = new ReportDocument();
crReportDocument.Load("c:\\reports\\Homes.rpt",CrystalDecisions.Shared.
OpenReportMethod.OpenReportByTempCopy);
crDatabase = crReportDocument.Database;
crTables = crDatabase.Tables;
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = "DSNName Or Server Name";
crConnectionInfo.DatabaseName = "HopeAndHome";
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "password";
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 aTable in crTables)
{
crTableLogOnInfo = aTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
aTable.ApplyLogOnInfo(crTableLogOnInfo);
}
}
}
}
return crReportDocument;
Keywords
sub report, Logon , Code net , KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , How To
Product
Crystal Reports 2008 V1 ; SAP Crystal Reports, developer version for Microsoft Visual Studio