title: Intermittent Issue - OleDbConnection.Open() throws System.Data.OleDb.OleDbException: Unspecified error
body: <p>A production application in our organization uses Excel 2003 files to handle user submitted data through a web application. This application works reliably most of the time. Recently, the application has begun intermittently throwing "System.Data.OleDb.OleDbException: Unspecified error" when the OleDbConnection.Open() method is called. The error continues until the application pool is recycled, at which time everything functions as expected again. No errors are captured within the Windows Application event log.</p>
  
  <p>The ASP.NET web application is hosted within web parts in WSS 3.0 on a Windows Server 2003 32 bit machine. The application is designed to prevent any concurrency issues. The system functional id is the only account with access to the temporary file store and there are mechanisms built in to ensure that the file cannot be overwritten by another upload during processing using a unique naming convention and upload tracking sub-system. </p>
  
  <p>Any insight would be much appreciated.</p>
  
  <p>We are using the following code to retrieve data from an Excel 2003 file:</p>
  
  <pre><code>public static DataTable GetWorksheetData(string filePath)
  {
      OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder { DataSource = filePath };
      builder.Provider = "Microsoft.Jet.OLEDB.4.0";
      builder["Extended Properties"] = "Excel 8.0;IMEX=1;HDR=YES";
  
      DataTable table = new DataTable();
      try
      {
          // Creates an OleDbConnection for the excel file
          using (OleDbConnection connection = new OleDbConnection(builder.ConnectionString))
          {
              connection.Open();
              string sheetName = GetWorksheet(connection, "Template");
              if (!string.IsNullOrEmpty(sheetName))
              {
                  using (OleDbCommand command = connection.CreateCommand())
                  {
                      try
                      {
                          command.CommandText = string.Format("SELECT * FROM [{0}]", sheetName);
  
                          using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
                          using (DataSet dataSet = new DataSet())
                          {
                              adapter.Fill(dataSet);
                                  table = dataSet.Tables[0];
                          }
                      }
                      finally
                      {
                          connection.Close();
                      }
                  }
              }
              else
              {
                  throw new InvalidWorksheetException();
              }
          }
      }
      catch (Exception ex)
      {
          Logger.Write(LogMsgSeverity.Error, ex);
          throw;
      }
      return table;
  }
  
  
  private static string GetWorksheet(OleDbConnection connection, string sheetName)
  {
      string validSheetName = string.Empty;
      using (DataTable tables = connection.GetSchema("Tables"))
      {
          DataRowCollection rows = tables.Rows;
  
          if (rows.Count &gt; 0)
          {
              foreach (DataRow row in rows)
              {
                  if (row["Table_Name"].ToString().Contains(sheetName))
                  {
                      validSheetName = sheetName;
                  }
              }
          }
      }
      return validSheetName;
  }
  </code></pre>