title: DataTable internal index is corrupted
body: <p>I am working with a .NET WinForms app in C#, running against the 3.5 .NET framework. In this app, I am setting the .Expression member of a <code>DataColumn</code> in a <code>DataTable</code>, like so:</p>
  
  <pre><code>DataColumn column = dtData.Columns["TestColumn"];
  column.Expression = "some expression";
  </code></pre>
  
  <p>The 2nd line, where I actually set <code>Expression</code>, will sometimes result in the following exception:</p>
  
  <pre><code>FileName=
  LineNumber=0
  Source=System.Data
  TargetSite=Int32 RBInsert(Int32, Int32, Int32, Int32, Boolean)
  System.InvalidOperationException: DataTable internal index is corrupted: '5'.
     at System.Data.RBTree`1.RBInsert(Int32 root_id, Int32 x_id, Int32 mainTreeNodeID, Int32 position, Boolean append)
     at System.Data.RBTree`1.RBInsert(Int32 root_id, Int32 x_id, Int32 mainTreeNodeID, Int32 position, Boolean append)
     at System.Data.Index.InitRecords(IFilter filter)
     at System.Data.Index.Reset()
     at System.Data.DataTable.ResetInternalIndexes(DataColumn column)
     at System.Data.DataTable.EvaluateExpressions(DataColumn column)
     at System.Data.DataColumn.set_Expression(String value)
  </code></pre>
  
  <p>There is no perceptible rhyme or reason as to when the error will occur; in loading the same data set, it may work fine but then reloading it will fail, and vice versa. This leads me to think it is related to a race condition, where another write operation is occurring on the <code>DataTable</code> as I'm trying to modify one of its columns. However, the code relating to <code>DataTable</code>s is <em>not</em> multi-threaded and runs only on the UI thread.</p>
  
  <p>I have searched the web and <a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/18544cd3-1083-45fe-b9e7-bb34482b68dd/#page:1" rel="noreferrer">Microsoft forums</a>, and there is much discussion and confusion over this issue. Back when the issue was first reported in 2006, the thought was that it was an flaw in the .NET framework, and there were some supposed hotfixes released that were presumably rolled into later versions of the .NET framework. However, people have reported mixed results in applying those hotfixes, which are no longer applicable to the current framework.</p>
  
  <p>Another prevailing theory is that there are operations on the DataTable which, though seemingly innocuous, are actually write operations. For example, creating a new <code>DataView</code> based on a <code>DataTable</code> is actually a write operation on the table itself, because it creates an internal index in the <code>DataTable</code> for later reference. These write operations are not thread-safe, so it sometimes happens that a race condition leads to an unthread-safe write coinciding with our access of the <code>DataTable</code>. This, in turn, causes the internal index of the <code>DataTable</code> to become corrupted, leading to the exception. </p>
  
  <p>I have tried putting <code>lock</code> blocks around each <code>DataView</code> creation in the code, but, as I mentioned before, code utilizing the <code>DataTable</code> is not threaded, and the <code>lock</code>s had no effect, in any case.</p>
  
  <p>Has anyone seen this and successfully solved / worked around it?</p>
  
  <hr>
  
  <p>No, unfortunately I can not. Loading the DataTable has already occurred by the time I get a hold of it to apply an Expression to one of its DataColumn's. I could remove the column and then re-add it using the code you suggested, but is there a particular reason why that would solve the internal index is corrupted problem?</p>