RSS

How to change the data source using CR for VS .NET application ??

13 Aug

<<Synopsis>>

How to change the data source using Crystal Report for VS .NET application ??

An application uses Crystal Reports for Visual Studio .NET as the reporting development tool. How do you change the database and server in a report at run-time?

For example, if a report is designed against the ‘pubs1‘ database and the ‘server1‘ server, how can you change the database to ‘pubs2‘ and the server to ‘server2‘?

1344854651_user_login

<<Solution>>

To change the database and server at runtime, copy one of the following code samples into your class (use the logon method defined in the code to change the database and server):

CAUTION:

  • When using ODBC, the DSN name is the server name. The DSN should be under the System DSN, and not under the User DSN.
  • The Logon method cannot change database file name when using DAO (Microsoft Access File/.mdb).
  • If you have a subreport that connects to a different database or server than your main report, use the ApplyLogon method instead of the Logon method. When using the ApplyLogon method, ensure you iterate through each subreports.
  • It is not possible to use the CR for VS .NET SDK to change a report from using SQL Authentication to Windows Authentication, or vice versa. This functionality is available through the Report Application Server (RAS) .NET SDK or Report Designer Component (RDC).

// Helper method that iterates through all tables in a report document
bool ApplyLogon(ReportDocument cr, ConnectionInfo ci)
{
TableLogOnInfo li;
// for each table apply connection info
foreach (Table tbl in cr.Database.Tables)
{
li = tbl.LogOnInfo;
li.ConnectionInfo = ci;
tbl.ApplyLogOnInfo(li);
// check if logon was successful
// if TestConnectivity returns false, check
// logon credentials
if (tbl.TestConnectivity())
{
// drop fully qualified table location
if (tbl.Location.IndexOf(“.”) > 0)
{
tbl.Location = tbl.Location.Substring(tbl.Location.LastIndexOf(“.”) + 1);
}
else
{
tbl.Location = tbl.Location;
}
}
else
{
return (false);
}
}
return(true);
}

//The Logon method iterates through all tables
bool Logon(ReportDocument cr, string server, string db, string id, string pass)
{
ConnectionInfo ci = new ConnectionInfo();
SubreportObject subObj;
ci.ServerName = server;
ci.DatabaseName = db;
ci.UserID = id;
ci.Password = pass;
if (!ApplyLogon(cr, ci))
{
return (false);
}
foreach (ReportObject obj in cr.ReportDefinition.ReportObjects)
{
if (obj.Kind == ReportObjectKind.SubreportObject)
{
subObj = (SubreportObject)obj;
if (!ApplyLogon(cr.OpenSubreport(subObj.SubreportName), ci)) return (false);
}
}
return (true);
}

 
Leave a comment

Posted by on August 13, 2012 in Crystal Reports, Visual C# . NET

 

Tags: , , , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: