sjmclarke
asked on
Select database for CR at runtime
I have embedded several Crystal reports into a .NET app, accessing them via a CrystalReportsViewer object. The reports were created using a test SQL Server database, but I want the .NET app to select a different database at runtime, based on a config file. I am using the code below to pass the config info into the report document. The code seems to work as far as it goes (the app complains if the specified database doesn't exist, for example), but the report then ignores it and still reports based on the original database with which it was created. All the databases are SQL Server. Is there something else I need to tell the reportdocument to get it to use the new database?
private void SetReportDatabase(ReportDo cument reportDoc)
{
TableLogOnInfo logonInfo = new TableLogOnInfo();
ConnectionInfo connectionInfo = new ConnectionInfo();
foreach(Table table in reportDoc.Database.Tables)
{
logonInfo = table.LogOnInfo;
connectionInfo = logonInfo.ConnectionInfo;
logonInfo.ConnectionInfo.S erverName = ConfigServer;
logonInfo.ConnectionInfo.D atabaseNam e = ConfigDatabase;
logonInfo.ConnectionInfo.U serID = ConfigUserid;
logonInfo.ConnectionInfo.P assword = ConfigPassword;
logonInfo.TableName = table.Name;
table.Location = table.Name;
table.ApplyLogOnInfo(logon Info);
bool b = table.TestConnectivity();
if (!b) MessageBox.Show ("Failed to logon to database");
}
crystalReportViewer.Report Source = reportDoc;
}
private void SetReportDatabase(ReportDo
{
TableLogOnInfo logonInfo = new TableLogOnInfo();
ConnectionInfo connectionInfo = new ConnectionInfo();
foreach(Table table in reportDoc.Database.Tables)
{
logonInfo = table.LogOnInfo;
connectionInfo = logonInfo.ConnectionInfo;
logonInfo.ConnectionInfo.S
logonInfo.ConnectionInfo.D
logonInfo.ConnectionInfo.U
logonInfo.ConnectionInfo.P
logonInfo.TableName = table.Name;
table.Location = table.Name;
table.ApplyLogOnInfo(logon
bool b = table.TestConnectivity();
if (!b) MessageBox.Show ("Failed to logon to database");
}
crystalReportViewer.Report
}
ASKER
Thanks. That solves the initial problem and the difference seems to be the line
MyTable.Location = MyTable.Location.Substring (MyTable.L ocation.La stIndexOf( ".")+1);
Without this line it just sticks on the original database. With it, the new database is picked up.
However, when I run a report a second time within the app (and SetConnection is called again), this line causes an exception to be thrown! Any ideas what is going on there?
MyTable.Location = MyTable.Location.Substring
Without this line it just sticks on the original database. With it, the new database is picked up.
However, when I run a report a second time within the app (and SetConnection is called again), this line causes an exception to be thrown! Any ideas what is going on there?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Glad to help
Regards
Emre
Regards
Emre
internal static void SetConnection(ref ReportDocument report, string serverName, string databaseName)
{
try
{
CrystalDecisions.Shared.Ta
foreach(CrystalDecisions.C
{
MyLogonInfo = MyTable.LogOnInfo;
if (MyTable.Name == "")
{
MyLogonInfo.ConnectionInfo
MyLogonInfo.ConnectionInfo
MyLogonInfo.ConnectionInfo
MyLogonInfo.ConnectionInfo
}
else
{
MyLogonInfo.ConnectionInfo
MyLogonInfo.ConnectionInfo
//MyLogonInfo.ConnectionIn
MyLogonInfo.ConnectionInfo
MyLogonInfo.ConnectionInfo
}
MyTable.Location = MyTable.Location.Substring
MyTable.ApplyLogOnInfo(MyL
}
}
catch( Exception logonInfoException )
{
throw logonInfoException;
}
}