[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 717
  • Last Modified:

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;
}
0
sjmclarke
Asked:
sjmclarke
  • 3
1 Solution
 
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
 
ebolekCommented:
Glad to help

Regards
Emre
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now