none
C# VSTO works from IDE but not from generated executable RRS feed

  • Question

  • Hello,

     

    I had a program which works perfectly well from my IDE but when I try to launch the generated executable it says the database is locked. I'm surprised there isn't the same reaction since I'm on the same machine (Windows XP, Visual Studio 2008)

     

    ...
    private static OleDbConnection connectionAccess = null;
    ...
    private OleDbConnection GetOleDbConnection()
      {
      if (connectionAccess == null)
        {
        OleDbConnectionStringBuilder builderCS = new OleDbConnectionStringBuilder();
        builderCS["Provider"] = "Microsoft.ACE.OLEDB.12.0";
    
        String databaseFullPath = Path.Combine(executionDirectory, MyConfigurationManager.DatabaseFileName);
    
        builderCS["Data Source"] = databaseFullPath;
        builderCS["User Id"] = "Admin";
        
        //Commented to test
        //builderCS.Add("Jet OLEDB:Database Locking Mode", 1);
    
        String connectionString = builderCS.ConnectionString;
    
        connectionAccess = new OleDbConnection(connectionString);
        }
      return connectionAccess;
      }
    ...
    public void ExportMicrosoftAccessDatabase2007Reports(Boolean icd)
      {
      String databaseFullPath = Path.Combine(executionDirectory, MyConfigurationManager.DatabaseFileName);
      databaseFullPath = Path.GetFullPath(databaseFullPath);
    
      if (System.IO.File.Exists(databaseFullPath))
        {
        String ReportDirectoryFullPath = Path.Combine(executionDirectory, MyConfigurationManager.ReportDirectory);
        if (!Directory.Exists(ReportDirectoryFullPath))
          {
          Directory.CreateDirectory(ReportDirectoryFullPath);
          }
    
        Access.ApplicationClass access = new Microsoft.Office.Interop.Access.ApplicationClass();
        access.Visible = false;
        access.OpenCurrentDatabase(databaseFullPath, true, null);
    
        try
          {
          Access.AllObjects reports = access.CurrentProject.AllReports;
          Access.Report report;
          Access.Controls controls;
    
          String reportName, PDFFullPath;
    
          if (reports.Count > 0)
            {
            foreach (Access.AccessObject ao in reports)
              {
              reportName = ao.Name;
              
              if (icd)
                {
                //TODO
                }
              else
                {
                switch (reportName.ToLower())
                  {
                  case "bat1":
                  case "bat2":
                  case "bat3":
                    PDFFullPath = Path.GetFullPath(Path.Combine(ReportDirectoryFullPath, reportName)) + ".pdf";
    
                    if (System.IO.File.Exists(PDFFullPath)) System.IO.File.Delete(PDFFullPath);
    
                    OleDbConnection connexion = GetOleDbConnection();
    
                    try
                      {
                      //Here Comes the error message (Files already in use) when using the generated exe file
                      //with the correct dlls copied in local
                      connexion.Open();
    
                      #region Record Number Count
                      String query;
                      OleDbCommand command;
                      int recordsCount = 0;
    
                      switch (reportName.ToLower())
                        {
                        case "bat1":
                          query = "select count(*) from MegaTable1Query";
                          command = new OleDbCommand(query, connexion);
                          recordsCount = (int)command.ExecuteScalar();
                          break;
                        case "bat2":
                          query = "select count(*) from MegaTable2Query";
                          command = new OleDbCommand(query, connexion);
                          recordsCount = (int)command.ExecuteScalar();
                          break;
                        case "bat3":
                          query = "select count(*) from MegaTable3Query";
                          command = new OleDbCommand(query, connexion);
                          recordsCount = (int)command.ExecuteScalar();
                          break;
                        default:
                          break;
                        }
                      #endregion
    
                      connexion.Close();
    
                      if (recordsCount > 0)
                        {
                        AllReports.Add(PDFFullPath);
    
                        access.DoCmd.OpenReport(reportName, Access.AcView.acViewPreview, null, null, Access.AcWindowMode.acHidden, null);
    
                        //Hide some parts in report
                        if (this.Count > 0 && !this[0].Nom.ToLower().StartsWith("test1"))
                          {
                          report = access.Reports[reportName];
                          controls = report.Controls;
    
                          foreach (Access.Control control in controls)
                            {
                            switch (control.Name.ToLower())
                              {
                              case "cocher390":
                              case "Étiquette391":
                                Access.Properties propriétés = control.Properties;
                                Access._AccessProperty propriété;
    
                                foreach (Object o in propriétés)
                                  {
                                  propriété = (Access._AccessProperty)o;
                                  if (propriété.Name.Equals("Visible")) propriété.Value = false;
                                  }
                                break;
                              default:
                                break;
                              }
                            }
                          }
    
                        access.DoCmd.OutputTo(Access.AcOutputObjectType.acOutputReport, reportName, Access.Constants.acFormatPDF, PDFFullPath, null, null, null);
                        access.DoCmd.Close(Access.AcObjectType.acReport, reportName, Access.AcCloseSave.acSaveNo);
                        }
                      }
                    catch (Exception e)
                      {
                      System.Console.WriteLine("Error : " + e);
                      }
                    break;
                  default:
                    break;
                  }
                }
              }
            }
          else
            {
            throw new Exception("No report to generate");
            }
          }
        catch (Exception e)
          {
          throw new Exception(e.Message);
          }
    
        access.CloseCurrentDatabase();
        access.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll);
        }
      else
        {
        throw new Exception("access database not found");
        }
      }
    ...
    

     

    Thanks in advance for any help.

    PS : full description of my problem on http://answers.microsoft.com/en-us/office/forum/office_2007-access/c-vsto-works-from-ide-but-not-from-generated/c3baa5c1-d44f-e011-8dfc-68b599b31bf5

    I had gone around it but I'd like to know why it didn't work...

    Monday, March 21, 2011 1:56 PM

Answers

  • Hi,

     

    Thanks for your reply,

     

    Surprisingly I don't have the problem anymore, with the exact same code, and it's troubling me cause I would have liked to know the reason. It would seem a lock was kept while using the executable file but not the IDE.

     

    On the IDE all worked just fine. On the other way I had an OleDbException : File in use with the executable. And using "wholockme" I knew it was true. So now I wonder why it worked in the IDE. To have better luck I had just added the appropriate Marshal.Release at the end of my code.

     

    Thus I guess the IDE deals better with bad programmers who forget to close their locks ;-)

    Monday, March 28, 2011 12:34 PM

All replies

  • Hi Guillaume,

    Thanks for posting in the MSDN Forum.

    It is based on my experience that your add-in application is able to connect to the Access database if you can connect in the Visual Studio IDE. Would you please show us the details of the exception message?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 23, 2011 5:53 AM
    Moderator
  • Hi,

     

    Thanks for your reply,

     

    Surprisingly I don't have the problem anymore, with the exact same code, and it's troubling me cause I would have liked to know the reason. It would seem a lock was kept while using the executable file but not the IDE.

     

    On the IDE all worked just fine. On the other way I had an OleDbException : File in use with the executable. And using "wholockme" I knew it was true. So now I wonder why it worked in the IDE. To have better luck I had just added the appropriate Marshal.Release at the end of my code.

     

    Thus I guess the IDE deals better with bad programmers who forget to close their locks ;-)

    Monday, March 28, 2011 12:34 PM
  • Hi Tom,

    Thanks for proposing as an anwser though I'm not sure where the problem comes from because even before I had added the Marshal.Release it worked without any change while I had spent days trying to understand why I had differents results in both the IDE and executable...

    At least this had answered my other thread about why Access was still invisible afterwards...

    Have a nice day!

    Wednesday, March 30, 2011 9:50 AM