3

I have used a static Global dataset shared between a number of threads.

I got the following exception :

Data Table internal index is corrupted: '5'.

In threading, I read the value from the datatable & update(used merge) the value in datatable both operation done in threading.

svick
  • 236,525
  • 50
  • 385
  • 514
vrushali
  • 31
  • 1
  • 1
  • 4

7 Answers7

3

You are doing operations on a dataset from different threads.

The dataset is not thread safe, you should make a wrapper class which protects the dataset from more than 1 operation at the time. This is called a mutex:

link to microsoft msdn

A better solution is to no use global state at all. This will fix your problem without any extra work and makes your code much more reliable.

Peter
  • 27,590
  • 8
  • 64
  • 84
1

If you are using threads with a dataset, that error will occur.

In my case, I was trying to create a new row for a dataset within a method that was running in threads.

One way was to use SyncLock around the method that creates the row or another way (and probably even better) was to create the rows outside of threads.

Basically my code looks something like this:

    Dim elements As New List(Of element)
    Dim dataRows As New List(Of MyDataSet.Row)

    For i As Integer = 0 To elements.Count - 1
        dataRows.Add(Me.Ds.Elements.NewElementsRow)
    Next

    Parallel.For(0, elements.Count, Sub(i As Integer)
                                        Me.CreateElementRow(elements(i), dataRows(i))
                                    End Sub)

In the CreateElementRow method I'm doing a lot of calculations in threads.

Hope this helps.

mrc
  • 307
  • 1
  • 4
  • 17
0

May be you are using same datatable in mutiple process @ same time.. I just resolved this issues using SYNCLOCK..

try this..

SyncLock your datatable

'''' -------your datatable process

End SyncLock

Kindly let me know if this helpful to you..

Happy coding..

Jon B
  • 51,025
  • 31
  • 133
  • 161
0

To prevent execution in multiple threads I used :

Application.Lock();
--- do your stuff in the datatable
Application.UnLock();
Allie
  • 1,081
  • 1
  • 13
  • 17
0
DataTable.BeginLoadData();

// Do MultiThreaded Inserts

DataTable.EndLoadData(); 
DataTable.AcceptChanges();

this will fix your issue

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
0

Well, Since you are using your dataTable in multiple threads, there is a high possibility of corruption--if you are manipulating the object from different threads.

Some pointers to help you resolve your issue :

  • Avoid using Default Views, and modifying the Default View if possible. Btw, .Net 2.0 has a number of reader/writer locks on creating views, so they are not the issue they were pre 2.0.
  • Call AcceptChanges() where possible.
  • Be careful about .Select(expression), since there is no reader/writer lock in this code -- and it is the only place (at least according to a person on the usenet so take it w/ a grain of salt -- however, this is very similar to your issue -- so using Mutexes may help)
  • Set AllowDBNull to the column in question (questionable value, but reported on the usenet -- I've used it only in places where it makes sense)
  • Make sure that you are not setting null (C#)/Nothing (VB) to a DataRow field. Use DBNull.Value instead of null. In your case you may wish to check that the field is not null, the expression syntax does supports the IsNull(val, alt_val) operator.
  • This has probably helped me the most (absurd as it sounds): If a value is not changing, don't assign it. For example if you want to assign value to some column do it as :
if (column.Expression != "some expression") 
      column.Expression = "some expression"; 

Answer source : StackOverflow - DataTable internal index is corrupted

Community
  • 1
  • 1
painotpi
  • 6,894
  • 1
  • 37
  • 70
0

This is how I fixed my Internal index is corrupted problem:

System.Data.DataTable dtNew = new DataTable();
for (int iCol = 0; iCol < dtOriginalData.Columns.Count; iCol++)
{
    dtNew.Columns.Add(dtOriginalData.Columns[iCol].ColumnName, dtOriginalData.Columns[iCol].DataType);
}
for (int iCopyIndex = 0; iCopyIndex < item.Data.Rows.Count; iCopyIndex++)
{
    dtNew.Rows.Add(dtOriginalData.Rows[iCopyIndex].ItemArray);
    //dtNew.ImportRow(dtOriginalData.Rows[iCopyIndex]);
}
dtOriginalData = dtNew;
Mario S
  • 11,715
  • 24
  • 39
  • 47