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;
}
sjmclarkeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ebolekCommented:
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;
                  }
            }
0
sjmclarkeAuthor Commented:
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?
0
ebolekCommented:
it should not . It never does it to me. You are right about the location property. If you dont set that your chnages are discarded. You should have another issue for the related message
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ebolekCommented:
Glad to help

Regards
Emre
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Crystal Reports

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.