Click here to Skip to main content
15,887,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have used the static Global dataset shared between number of thread.
i got the exception as "Data Table internal index is corrupted: '5'."
In the threading ,read the value from datatable & update(used merge) the value in datatable both operation done in threading.


Please help me
thanks in advance
Posted
Updated 2-Nov-18 8:03am
v2

You are not alone to suffer from this problem. Have a look at the below links:
1. http://stackoverflow.com/questions/450675/datatable-internal-index-is-corrupted[^]
2. http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/18544cd3-1083-45fe-b9e7-bb34482b68dd[^]

The first link does provide some sort of workaround.

I believe you are synchronizing (using locks etc.) your threads while accessing datatable. If not please do that first.
 
Share this answer
 
I use the solution 1 of RakeshMeena

This is my code:

DataTable dt = new DataTable();
dt.Columns.Add("secuencia", typeof(System.Int32));
dt.Columns.Add("comercio", typeof(System.Int32));
dt.Columns.Add("fecha", typeof(System.DateTime));
dt.Columns.Add("valor", typeof(System.Decimal));
dt.Columns.Add("propina", typeof(System.Decimal));
dt.Columns.Add("iva", typeof(System.Decimal));
dt.Columns.Add("total", typeof(System.Decimal));
DataRow dr = null;

dr = dt.NewRow();dr["secuencia"] = 1; dr["comercio"] = 1; dr["fecha"] = DateTime.Now; dr["valor"] = 32.21; dr["propina"] = 0.12; dr["iva"] = 0.41; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 2; dr["comercio"] = 2; dr["fecha"] = DateTime.Now; dr["valor"] = 67.43; dr["propina"] = 0.48; dr["iva"] = 0.25; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 3; dr["comercio"] = 3; dr["fecha"] = DateTime.Now; dr["valor"] = 51.76; dr["propina"] = 0.54; dr["iva"] = 0.39; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 4; dr["comercio"] = 1; dr["fecha"] = DateTime.Now; dr["valor"] = 65.09; dr["propina"] = 0.67; dr["iva"] = 0.43; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 5; dr["comercio"] = 4; dr["fecha"] = DateTime.Now; dr["valor"] = 109.59; dr["propina"] = 0.91; dr["iva"] = 0.55; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 6; dr["comercio"] = 2; dr["fecha"] = DateTime.Now; dr["valor"] = 93; dr["propina"] = 0.84; dr["iva"] = 0.76; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 7; dr["comercio"] = 4; dr["fecha"] = DateTime.Now; dr["valor"] = 93.09; dr["propina"] = 0.43; dr["iva"] = 0.97; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 8; dr["comercio"] = 2; dr["fecha"] = DateTime.Now; dr["valor"] = 41.72; dr["propina"] = 0.97; dr["iva"] = 0.75; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 9; dr["comercio"] = 1; dr["fecha"] = DateTime.Now; dr["valor"] = 87.54; dr["propina"] = 0.19; dr["iva"] = 0.81; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 10; dr["comercio"] = 3; dr["fecha"] = DateTime.Now; dr["valor"] = 98.32; dr["propina"] = 0.27; dr["iva"] = 0.83; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 11; dr["comercio"] = 3; dr["fecha"] = DateTime.Now; dr["valor"] = 124.65; dr["propina"] = 0.37; dr["iva"] = 0.89; dr["total"] = 35.79; dt.Rows.Add(dr);
dr = dt.NewRow();dr["secuencia"] = 12; dr["comercio"] = 4; dr["fecha"] = DateTime.Now; dr["valor"] = 48.28; dr["propina"] = 0.62; dr["iva"] = 0.58; dr["total"] = 35.79; dt.Rows.Add(dr);

DataTable dt2 = new DataTable();
dt2.Columns.Add("secuencia", typeof(System.Int32));
dt2.Columns.Add("comercio", typeof(System.Int32));
dt2.Columns.Add("fecha", typeof(System.DateTime));
dt2.Columns.Add("valor", typeof(System.Decimal));
dt2.Columns.Add("propina", typeof(System.Decimal));
dt2.Columns.Add("iva", typeof(System.Decimal));
dt2.Columns.Add("total", typeof(System.Decimal));

ParallelOptions pOpciones = new ParallelOptions();
pOpciones.MaxDegreeOfParallelism = 4; //Maximo grado de paralelismo; en este Caso 4 threads
Parallel.ForEach(dt.AsEnumerable(), pOpciones, drow =>
{
    //*******************************************
    lock (dt2) // I use lock for every thread ;)
    //*******************************************
    {
        DataRow dr2 = dt2.NewRow();
        dr2["secuencia"] = drow["secuencia"];
        dr2["comercio"] = drow["comercio"];
        dr2["fecha"] = drow["fecha"];
        dr2["valor"] = drow["valor"];
        dr2["propina"] = drow["propina"];
        dr2["iva"] = drow["iva"];
        dr2["total"] = drow["total"];
        dt2.Rows.Add(dr2);
    }
});

string sec = "";
string valor = "";
rtbOrigen.Text = "";
foreach (DataRow fl in dt.Rows)
{
    sec = fl["secuencia"].ToString();
    if (sec.Length == 1)
        sec = " " + sec;
    valor = fl["valor"].ToString();
    if (valor.Length < 10)
        valor = new string(' ', 10 - valor.Length) + valor;

    rtbOrigen.Text += sec + " " +
                      fl["comercio"].ToString() + " " +
                      ((DateTime)fl["fecha"]).ToString("yyyy-MM-dd HH:mm:ss.fff") + " " +
                      valor + " " +
                      fl["propina"].ToString() + " " +
                      fl["iva"].ToString() + " " +
                      fl["total"].ToString() + "\r\n";
}
rtbCopia.Text = "";

//*********************************************************************
//In this foreach I get the error when I don't use the lock statement
//*********************************************************************
foreach (DataRow fl2 in dt2.Select("", "secuencia"))
{
    sec = fl2["secuencia"].ToString();
    if (sec.Length == 1)
        sec = " " + sec;

    valor = fl2["valor"].ToString();
    if (valor.Length < 10)
        valor = new string(' ', 10 - valor.Length) + valor;

    rtbCopia.Text += sec + " " +
                      fl2["comercio"].ToString() + " " +
                      ((DateTime)fl2["fecha"]).ToString("yyyy-MM-dd HH:mm:ss.fff") + " " +
                      valor + " " +
                      fl2["propina"].ToString() + " " +
                      fl2["iva"].ToString() + " " +
                      fl2["total"].ToString() + "\r\n";
}
 
Share this answer
 
v3
Comments
CHill60 15-Jun-14 7:00am    
So this is the same as solution 1 then? Or are you posting this against a 3 year old question because you have a problem with it?
JimVas2005 16-Jun-14 12:07pm    
I have the problem: DataTable internal index is corrupted: '5'.used in threading
I read the solution 1, and I put my code with the change suggested in solution 1
I know it's an old Thread but here's the fix

C#
DataTable.BeginLoadData();

// Do MultiThreaded Inserts

DataTable.EndLoadData(); 
DataTable.AcceptChanges();
 
Share this answer
 
Comments
Simple Pudding 3-Nov-18 1:21am    
In my case, I did as follows.

DataTable.BeginLoadData();
lock(lockObj){
// Do MultiThreaded read/write,
}
DataTable.EndLoadData();
try using SYNCLOCK before adding ,updating deleting row from datatable
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900