0

I have a line of code that should clear a datatable if the rowcount > 0. However despite the rowcount being 0, the clear line is executed:

enter image description here

I'm experiencing some very odd behaviour with this (inherited) application and have investigated whether the build is working correctly (I've cleaned and rebuilt many times, and made sure all assemblies are x86) and turned off code optimization as per this and this because I feared that my pdb files were out of sync with my code, however nothing has helped.

I'm totally stumped as to why this line of code is executing and would very much appreciate any advice.

Edit: dt is System.Data.Datatable, the exception detail is:

System.Data.RowNotInTableException occurred HResult=-2146232024 Message=This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row. Source=System.Data StackTrace: at System.Data.DataRow.GetDefaultRecord() InnerException:

majjam
  • 1,286
  • 2
  • 15
  • 32
  • what type is dt? is it a vanilla datatable? https://msdn.microsoft.com/en-us/library/system.data.datatable%28v=vs.110%29.aspx – basher Mar 20 '15 at 15:34
  • 2
    This could be a race condition. Is it possible that another thread has modified the table after you've fetched it? – Adi Lester Mar 20 '15 at 15:42
  • Agreeing with Adi. Also there might be an event on the datatable/datarowcollection. 'when count is checked, return original coutn then clear items to screw with the next guy' – basher Mar 20 '15 at 15:45
  • Thanks very much for the suggestions, any idea how I'd investigate the race condition? I'll check the event possibility, but this app has a lot of crazy threading so a race condition sounds likely. – majjam Mar 20 '15 at 15:46
  • 4
    `DataTable.Clear()` does not throw `RowNotInTableException` so, you are probably not debugging what you think you are, i.e. your code does not match your binary. – Jodrell Mar 20 '15 at 15:52
  • I've tested your idea Jodrell with a Console.Writeline just above the dt.Clear() call, and that fires - so those changes at least are reflected in my binary. I've also worked my way through the suggestions here: http://stackoverflow.com/questions/2322876/debugging-information-cannot-be-found-or-does-not-match-visual-studios so with regret, am leaning back to the race condition. – majjam Mar 20 '15 at 16:27

1 Answers1

0

I've worked around the above issue for now by removing and re-adding the datatable instead of using the DataSet.Tables().Clear function:

    If ClearTable And DSet.Tables(TableNm).Rows.Count > 0 Then
        DSet.Tables.Remove(TableNm)
        DSet.Tables.Add(TableNm)
    End If
majjam
  • 1,286
  • 2
  • 15
  • 32