I was having considerable problem figuring out a way to specify the connection parameters for my Crystal Reports at runtime that used the Pull model for stuffing data. The problem was that I needed to use a different runtime connection than what was configured for the report at design time. And everytime I did so, the report threw up the form asking the user to fill in the connection parameters (I was using ASP.NET web-based reports but the approach below works on desktop also). Needless to say, this was utterly unprofessional.

My understanding was that using the ReportDocument‘s SetDatabaseLogon method should have been sufficient for the task. However, this method only worked when the Server & Database at design & run times were same. If they were different, the Report Viewer again threw up the form asking the credentials.

And I finally got what I was looking for at the SAP Crystal Reports forums here, thanks to the SAP community member, Ludek Uher:
http://forums.sdn.sap.com/thread.jspa?messageID=8836569

So, the piece in the puzzle that I was missing was, that you need to set the Database Logon credentials on each table inside the Report database independently, if your design & run time connections differ. However, if they don’t, then simply using the SetDatabaseLogin method should be sufficient to prevent the credentials form from being thrown up. Looking at it retrospectively, I now think that it makes sense to do so for each table. So, here’s the code you can employ in case your design & run time connection strings differ for the Crystal Report:

 

{syntaxhighlighter brush: csharp;fontsize: 100; first-line: 1; }//The code below is for ASP.NET Crystal Reports. So, just need to replace namespaces appropriately for Desktop based reports.

CrystalDecisions.Web.CrystalReportSource source = new CrystalDecisions.Web.CrystalReportSource();
source.Report = new CrystalDecisions.Web.Report() { FileName = “Path to report .rpt file” };

ConnectionInfo c=new ConnectionInfo() { ServerName = “ServerName”, DatabaseName = “DB Name”, UserID = “Username”, Password = “Password” };
foreach (CrystalDecisions.CrystalReports.Engine.Table t in source.ReportDocument.Database.Tables)
{
TableLogOnInfo crTableLogOnInfo = t.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = c;
t.ApplyLogOnInfo(crTableLogOnInfo);
}
source.ID = “TripReport”;

//As I was creating this Report source dynamically, I needed to add the Source control to the page. You can skip this step, if you dont require it.
this.pnlReport.Controls.Add(this.source);

this.CrystalReportViewer1.ReportSource = this.source.ReportDocument;
{/syntaxhighlighter}