title: Change the database path for a Crystal Report dynamically through C# code when the form with the report viewer loads
body: <p>I have a simple application that connects to an Access database (.mdb). I connected the database file to the application using code only. The connection string is as follows,</p>
  
  <p><strong>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\CentreDB.mdb;Jet OLEDB:Database Password=pass"</strong></p>
  
  <p>For designing purposes I connected the database file to a crystal report by using the Connect Database File option in the report Wizard. But I need to use code to dynamically assign the database to the crystal report, so that I wont have a problem when I deploy it in a client machine. I tried to use this code below.</p>
  
  <pre><code>public void ViewReport(FileInfo crFileInfo) // path\filename.rpt
          {
              ConnectionInfo crConnectionInfo = new ConnectionInfo();
              crConnectionInfo.ServerName = "YOUR SERVER NAME";
              crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";
              crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
              crConnectionInfo.Password = "YOUR DATABASE PASSWORD";
  
              // CrystalReportViewer control
              crViewer.ReportSource = ReportSourceSetup(crFileInfo, crConnectionInfo);
              crViewer.Refresh();
          }
  
          ReportDocument ReportSourceSetup(FileInfo crFileInfo, ConnectionInfo crConnectionInfo)
          {
              ReportDocument crDoc = new ReportDocument();
              TableLogOnInfos crTableLogonInfos = new TableLogOnInfos();
              TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
              Tables crTables;
  
              crDoc.Load(crFileInfo.FullName);
  
              // Each table in report needs to have logoninfo setup:
              crTables = crDoc.Database.Tables;
              foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
              {
                  crTableLogonInfo = crTable.LogOnInfo;
                  crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                  crTable.ApplyLogOnInfo(crTableLogonInfo);
              }
  
              return crDoc;
          } 
  </code></pre>
  
  <p>I found this sample on the net, but I can't get it to work. I need to know where I need to place this exactly &amp; what I should pass for "YOUR SERVER NAME", "YOUR DATABASE NAME" since I'm using a database file with only a password. I think that the FileInfo object carries the path of the database. Please help me if possible. Any help would be greatly appreciated.</p>