You are hereBlogs / rahul's blog / Specifying Run time Connection parameters for Crystal Reports using Pull Model

Specifying Run time Connection parameters for Crystal Reports using Pull Model


rahul's picture

By rahul - Posted on 08 March 2010

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:

 

//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;

 

thanks for the article.....

Post new comment

The content of this field is kept private and will not be shown publicly.

Mollom CAPTCHA (play audio CAPTCHA)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.