title: 1627 - Microsoft.Deployment.WindowsInstaller.InstallerException: Function failed during execution
body: <p>Now, the issue is, when I run this code on my created MSI file, then the last query i.e the query for the delayed start becomes failed and gives me the exception with msi errorcode 1627:</p>
  
  <p>Microsoft.Deployment.WindowsInstaller.InstallerException: Function failed during execution.
     at Microsoft.Deployment.WindowsInstaller.View.Execute(Record executeParams)
     at Microsoft.Deployment.WindowsInstaller.Database.Execute(String sql, Record record)
     at Microsoft.Deployment.WindowsInstaller.Database.Execute(String sqlFormat, Object[] args)
     at MSIManager.Program.UpdateMSIService(String msi, String serviceName, String displayName, String fileName) in C:\Users\Chirag Pathak\3D Objects\bizbrain-agent-v7\Installers\MSIManager\Program.cs:line 96</p>
  
  <p>Below is my code </p>
  
  <pre><code>   class Program
              {
                  private static int msidbServiceConfigEventInstall = 1;
                  private static int SERVICE_CONFIG_DELAYED_AUTO_START = 3;
  
                  private static string BIN_FILE_PATH_X64 = @"C:\Users\Chirag Pathak\3D Objects\bizbrain-agent-v7\x64\Debug\";
                  private static string MSI_NAME_X64 = "MyTallyApp Agent Installer (x64).msi";
  
                  private static string PATH_PREFIX = "C:\\Users\\Chirag Pathak\\3D Objects\\bizbrain-agent-v7\\Installers\\Installers\\";
  
          static void Main(string[] args)
                  {
                      UpdateMSIService(MSI_NAME_X64, "BizBrainAgentWindowsService-x64", "MyTallyApp Agent Service (64 bit)", "BizBrainAgentWindowsService-x64.exe");
                      UpdateMSIService(MSI_NAME_X64, "BizBrainAgentWindowsServiceLauncher-x64", "MyTallyApp Agent Service Launcher (64 bit)", "BizBrainAgentWindowsServiceLauncher-x64.exe");
  
                  }
  
                static bool UpdateMSIService(string msi, string serviceName, string displayName, string fileName)
                  {
                      string databasePath = PATH_PREFIX + msi;
                      if (!File.Exists(databasePath))
                      {
                          Console.WriteLine("MSI File " + msi + " does not exist!");   
                          return false;
                      }
  
                      using(Database database = new Database(databasePath, DatabaseOpenMode.Direct))
                      {
                          try
                          {
                              serviceName = "'" + serviceName + "'";
                              displayName = "'" + displayName + "'";
  
                              Microsoft.Deployment.WindowsInstaller.View view = database.OpenView("Select Component_,FileName from File");
                              view.Execute();
                              Record[] records = view.ToArray&lt;Record&gt;();
                              string componentId = "'" + GetComponentId(records, fileName) + "'";
                              view.Close();
                              Console.WriteLine("Service ComponentId=:" + componentId);
                              //Do this after the search
                              fileName = "'" + fileName + "'";
  
                              string sqlDeleteServiceInstall = "DELETE FROM `ServiceInstall` WHERE `ServiceInstall`=" + serviceName;
                              database.Execute(sqlDeleteServiceInstall);
  
  
                              string sqlInsertServiceInstall = "INSERT INTO `ServiceInstall` (`ServiceInstall`,`Name`,`DisplayName`,`ServiceType`,`StartType`,`ErrorControl`,`Component_`,`Description`) VALUES (" + serviceName + "," + serviceName + "," + displayName + ",16,2,1," + componentId + "," + displayName + ")";
                              database.Execute(sqlInsertServiceInstall);
  
                              string sqlDeleteServiceControl = "DELETE FROM `ServiceControl` WHERE `ServiceControl`=" + serviceName;
                              database.Execute(sqlDeleteServiceControl);
  
                              string sqlInsertServiceControl = "INSERT INTO `ServiceControl` (`ServiceControl`,`Name`,`Event`,`Component_`) VALUES(" + serviceName + "," + serviceName + ",1," + componentId + ")"; 
                              database.Execute(sqlInsertServiceControl);
  
  
                    ****//For Automatic [Delayed Start]
                                  string sqlInsertMSIServiceConfig = "INSERT INTO `MsiServiceConfig`(`MsiServiceConfig`,`Name`,`Event`,`ConfigType`,`Argument`,`Component_`) VALUES('AutoStartDelayed'," + serviceName + "," + msidbServiceConfigEventInstall + "," + SERVICE_CONFIG_DELAYED_AUTO_START + ",1," + componentId + ")";  
                                  database.Execute(sqlInsertMSIServiceConfig);//****//This is the line where exception occures.
                              return true;
  
                          }
                          catch (Exception e)
                          { }
                      }  
                  }
  </code></pre>
  
  <p>{"msiErrorCode":1627,"ClassName":"Microsoft.Deployment.WindowsInstaller.InstallerException","Message":"Function failed during execution.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":"   at Microsoft.Deployment.WindowsInstaller.View.Execute(Record executeParams)\r\n   at Microsoft.Deployment.WindowsInstaller.Database.Execute(String sql, Record record)\r\n   at Microsoft.Deployment.WindowsInstaller.Database.Execute(String sqlFormat, Object[] args)\r\n   at MSIManager.Program.UpdateMSIService(String msi, String serviceName, String displayName, String fileName) in C:\Users\Chirag Pathak\3D Objects\bizbrain-agent-v7\Installers\MSIManager\Program.cs:line 96","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8\nExecute\nMicrosoft.Deployment.WindowsInstaller, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ce35f76fcda82bad\nMicrosoft.Deployment.WindowsInstaller.View\nVoid Execute(Microsoft.Deployment.WindowsInstaller.Record)","HResult":-2146233087,"Source":"Microsoft.Deployment.WindowsInstaller","WatsonBuckets":null}</p>