Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.08 Subreport Data Sources

Working with Subreports

As mentioned earlier in the chapter, when you use the SetDatabaseLogin() method to pass security credentials to a report, it logs into the main report’s database as well as the database use by the subreports. Thus, you don’t have to do anything extra to open a report that also uses subreports.

If you want to have more control over the database objects for a report, you need to reference them directly. This will let you do such things as change the data source. Fortunately, the subreport object is based on the same class as the main report (the ReportDocument class). Modifying the properties of a subreport uses the same code that was written for the main report. The only difference being that you have to use theOpenSubreport() method to get a reference to the subreport object.

Listing 17-5. Set subreport credentials
[VB.NET]
Dim myMainReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim mySubReport As CrystalDecisions.CrystalReports.Engine.ReportDocument
myMainReport.Load("report.rpt")
mySubReport = myMainReport.OpenSubreport("subreport")
… insert your code to modify the SubReport object properties….
CrystalReportViewer1.ReportSource = myMainReport
[C#]
CrystalDecisions.CrystalReports.Engine.ReportDocument myMainReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
CrystalDecisions.CrystalReports.Engine.ReportDocument mySubReport;
myMainReport.Load("report.rpt");
mySubReport = myMainReport.OpenSubreport("subreport");
CrystalReportViewer1.ReportSource = myMainReport;