title: Operation must use an updateable query - Excel 2007 file as OleDB Data source
body: <p>I often get this error while trying to update a field(s) in an excel sheet using OLEDB.</p>
  
  <p>This does work sometimes, but I often get this error. (Operation must use an updateable query)</p>
  
  <p>Connection Code:</p>
  
  <pre><code>string filepath = "c:\\SampleFile.xls";
  OleDbConnection MyConnection;
  MyConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + filepath + "';Extended Properties=Excel 8.0;");
  con3 = MyConnection;
  MyConnection.Open();
  if (MyConnection.State != ConnectionState.Open)
  {
      do
      {
          Thread.Sleep(50);
      } while (MyConnection.State != ConnectionState.Open);
  }
  </code></pre>
  
  <p>Here is the code that performs the update:</p>
  
  <pre><code>linesx = "231,214,412"; //this is variable
  string sqlx = string.Format("Update [Resolved Results$] set [Audit Result] = 'AUDITED' where [LineNo] IN ({0})", linesx);
  OleDbCommand myCommand = new OleDbCommand();
  myCommand.Connection = MyConnection;
  myCommand.CommandText = sqlx;
  try
  {
      Console.WriteLine(sqlx);
      myCommand.ExecuteNonQuery();
  }
  catch (Exception ex)
  {
      updRes = "Failed! :("+Environment.NewLine+"ERROR: " + ex.Message + Environment.NewLine;                        
  } 
  </code></pre>
  
  <p><strong>NOTE: This sometimes works and sometimes not, for the SAME value of linesx</strong> </p>
  
  <p>I also monitor the status of connections to make sure that the connection is closed before it is being opened, and that the connection is open before the command is executed. I have tried multiple ways to solved it including : having a separate connection for read (con1) and update commands(MyConnection ), (ofcourse, I make sure con1 is closed before I open MyConnection)</p>
  
  <p>I have researched plenty about this issue and the answer that i could get the most is that there is no write permissions for the file. HOwever, the program does have write permissions (As the update command does work sometimes and sometimes not)</p>
  
  <p>Sometimes the update command does work after it has failed once, (I.e i try to update twice or thrice) and sometimes it doesnt.</p>
  
  <p>Why does this happen?? How do I fix this??</p>
  
  <p>UPDATE 1:
  I tried using <strong>Microsoft.ACE.OLEDB.12.0</strong> insted of the JET - The error doesnt appear but it doesnt update the table either!!!</p>
  
  <p>UPDATE 2:
  i was using multiple connections to the same file, which might have caused the problem. the connection was a global variable.
  I changed the whole application architecture to initialize the connection only when required, like <strong>Damith</strong> said and it worked like a charm (It works 99% of the times(
  It was well worth the time i took to implement this change in my entire application.</p>
  
  <p>Thank you very much <strong>Damith</strong></p>
  
  <p>I hope this helps the other 2^n people who are facing the same problem :D</p>
  
  <p>UPDATE 3:
  I think i know to fix the other 1% - there are functions that create multiple threads, and each thread opens its own connection - maybe that's causing the problem. The solution is to make sure there is one and only one connection to the excel DB. (not necessarily ConnectionState.Close, but the con variable should be out of scope and null)</p>