Link to home
Start Free TrialLog in
Avatar of sjmclarke
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(ReportDocument reportDoc)
{      
      TableLogOnInfo logonInfo = new TableLogOnInfo();
      ConnectionInfo connectionInfo = new ConnectionInfo();
      
      foreach(Table table in reportDoc.Database.Tables)
      {
            
            logonInfo = table.LogOnInfo;
            connectionInfo = logonInfo.ConnectionInfo;
            
            logonInfo.ConnectionInfo.ServerName = ConfigServer;
            logonInfo.ConnectionInfo.DatabaseName = ConfigDatabase;
            logonInfo.ConnectionInfo.UserID = ConfigUserid;
            logonInfo.ConnectionInfo.Password = ConfigPassword;
            logonInfo.TableName = table.Name;
            table.Location = table.Name;
            
            table.ApplyLogOnInfo(logonInfo);
            bool b = table.TestConnectivity();
            if (!b) MessageBox.Show ("Failed to logon to database");      
      }
      crystalReportViewer.ReportSource = reportDoc;
}
Avatar of ebolek
ebolek

I do it like this. It works. This is for integrated security but if you pass username and password, it can be used for sql authentication as well.

internal static void SetConnection(ref ReportDocument report, string serverName, string databaseName)
            {
                  try
                  {
                        CrystalDecisions.Shared.TableLogOnInfo MyLogonInfo;
                        foreach(CrystalDecisions.CrystalReports.Engine.Table MyTable in report.Database.Tables)
                        {
                   
                              MyLogonInfo = MyTable.LogOnInfo;
                              if (MyTable.Name == "")
                              {
                                    MyLogonInfo.ConnectionInfo.ServerName  = serverName;
                                    MyLogonInfo.ConnectionInfo.DatabaseName = databaseName;
                                    MyLogonInfo.ConnectionInfo.UserID = ""; //in integrated security, this is problem.
                                    MyLogonInfo.ConnectionInfo.Password  = "";      //it works when it is not changed
                              }
                              else
                              {                  
                              
                                     
                                    MyLogonInfo.ConnectionInfo.ServerName  = serverName;
                                    MyLogonInfo.ConnectionInfo.DatabaseName = databaseName;
                                    //MyLogonInfo.ConnectionInfo.AllowCustomConnection  = true;
                                    MyLogonInfo.ConnectionInfo.UserID  = "";
                                    MyLogonInfo.ConnectionInfo.Password = "";                              
                                     
                              }
                              MyTable.Location = MyTable.Location.Substring(MyTable.Location.LastIndexOf(".")+1);
                              MyTable.ApplyLogOnInfo(MyLogonInfo);            
                                
                        }                        
                  }
                  catch( Exception logonInfoException )
                  {
                        throw logonInfoException;
                  }
            }
Avatar of sjmclarke

ASKER

Thanks. That solves the initial problem and the difference seems to be the line
         MyTable.Location = MyTable.Location.Substring(MyTable.Location.LastIndexOf(".")+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?
ASKER CERTIFIED SOLUTION
Avatar of ebolek
ebolek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad to help

Regards
Emre