Now let's look into setting the connection info using the
Crystal Viewer object. This is great if you are always using the same report
in the Crystal Viewer object. If you want to use the Viewer to display more
than one report, the Report Document method probably makes more sense to use.
See Listing 2 for the C# code that sets the connection information on
CrystalViewer1.
Listing 2
CrystalDecisions.Shared.ConnectionInfo crDbConnection = new
CrystalDecisions.Shared.ConnectionInfo();
crDbConnection.IntegratedSecurity = true;
crDbConnection.DatabaseName = "TfsWorkItemTracking";
crDbConnection.ServerName = "tfdbserver";
CrystalDecisions.CrystalReports.Engine.ReportDocument oRpt =
(CrystalDecisions.CrystalReports.Engine.ReportDocument)
crystalReportViewer1.ReportSource;
CrystalDecisions.CrystalReports.Engine.Database crDatabase = oRpt.Database;
CrystalDecisions.Shared.TableLogOnInfo oCrTableLoginInfo;
foreach (CrystalDecisions.CrystalReports.Engine.Table oCrTable in
crDatabase.Tables)
{
oCrTableLoginInfo = oCrTable.LogOnInfo;
oCrTableLoginInfo.ConnectionInfo = crDbConnection;
oCrTable.ApplyLogOnInfo(oCrTableLoginInfo);
}
As we can see, the connection information setting is pretty
much the same. We actually do use a Report Document object to help in setting
the database. We do this by Casting the report viewer reportsource to the
ReportDocument object. Once that is done, everything else is similar to the
last section.