PC Review


Reply
Thread Tools Rate Thread

what is the best way to make a DataTable thread safe? DataTable.AcceptChanges() will throw exception (not documented)

 
 
Ryan Liu
Guest
Posts: n/a
 
      7th Jun 2006
Hi,

In the .NET Framework SDK documentation, I can see DataRow.AcceptChanges
method will throw RowNotInTableException exeception.

And in DataTable.AcceptChanges(), the documentation does not mention it will
throw any exception, but in my code (multi-thread), I see it throw
exceptions at two situations:

dr = this.currentQuotaUserDt.NewRow();

this.currentQuotaUserDt.Rows.Add(dr);

this.currentQuotaUserDt.AcceptChanges();

Last line throw exception : Cannot perform this operation on a row not in
the table.

(Code is more complex than what list here and it is multi-thread
enviorment.)

==============
this.currentDr.Delete();
this.currentDr.Table.AcceptChanges();

The last line will throw:
System.Data.RowNotInTableException: Cannot perform this operation on a row
not in the table.
at System.Data.DataTable.SetOldRecord(DataRow row, Int32 proposedRecord)
at System.Data.DataTable.CommitRow(DataRow row)
at System.Data.DataTable.AcceptChanges()



Thanks,
Ryan


 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      7th Jun 2006
Ryan,
It is not clearly evident from your post how you could have gotten the
exception you report, but if you want to ensure thread safety, you can use
the lock statement on a local object variable:

object locker = new object();
lock (locker)
{

// your code here


}

--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Ryan Liu" wrote:

> Hi,
>
> In the .NET Framework SDK documentation, I can see DataRow.AcceptChanges
> method will throw RowNotInTableException exeception.
>
> And in DataTable.AcceptChanges(), the documentation does not mention it will
> throw any exception, but in my code (multi-thread), I see it throw
> exceptions at two situations:
>
> dr = this.currentQuotaUserDt.NewRow();
>
> this.currentQuotaUserDt.Rows.Add(dr);
>
> this.currentQuotaUserDt.AcceptChanges();
>
> Last line throw exception : Cannot perform this operation on a row not in
> the table.
>
> (Code is more complex than what list here and it is multi-thread
> enviorment.)
>
> ==============
> this.currentDr.Delete();
> this.currentDr.Table.AcceptChanges();
>
> The last line will throw:
> System.Data.RowNotInTableException: Cannot perform this operation on a row
> not in the table.
> at System.Data.DataTable.SetOldRecord(DataRow row, Int32 proposedRecord)
> at System.Data.DataTable.CommitRow(DataRow row)
> at System.Data.DataTable.AcceptChanges()
>
>
>
> Thanks,
> Ryan
>
>
>

 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      7th Jun 2006
Peter,

That's assuming that the data table is only being accessed in that
method, and that method is the only one that is being called from other
threads.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Peter Bromberg [C# MVP]" <(E-Mail Removed)> wrote in message
news:8D45ECF4-21C0-41A6-AF22-(E-Mail Removed)...
> Ryan,
> It is not clearly evident from your post how you could have gotten the
> exception you report, but if you want to ensure thread safety, you can use
> the lock statement on a local object variable:
>
> object locker = new object();
> lock (locker)
> {
>
> // your code here
>
>
> }
>
> --Peter
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "Ryan Liu" wrote:
>
>> Hi,
>>
>> In the .NET Framework SDK documentation, I can see DataRow.AcceptChanges
>> method will throw RowNotInTableException exeception.
>>
>> And in DataTable.AcceptChanges(), the documentation does not mention it
>> will
>> throw any exception, but in my code (multi-thread), I see it throw
>> exceptions at two situations:
>>
>> dr = this.currentQuotaUserDt.NewRow();
>>
>> this.currentQuotaUserDt.Rows.Add(dr);
>>
>> this.currentQuotaUserDt.AcceptChanges();
>>
>> Last line throw exception : Cannot perform this operation on a row not
>> in
>> the table.
>>
>> (Code is more complex than what list here and it is multi-thread
>> enviorment.)
>>
>> ==============
>> this.currentDr.Delete();
>> this.currentDr.Table.AcceptChanges();
>>
>> The last line will throw:
>> System.Data.RowNotInTableException: Cannot perform this operation on a
>> row
>> not in the table.
>> at System.Data.DataTable.SetOldRecord(DataRow row, Int32
>> proposedRecord)
>> at System.Data.DataTable.CommitRow(DataRow row)
>> at System.Data.DataTable.AcceptChanges()
>>
>>
>>
>> Thanks,
>> Ryan
>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      7th Jun 2006
Nick,
Absolutely; in the interest of completeness, the OP would have to "protect"
any method that accesses the datatable and which could possibly be called by
multiple threads.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Nicholas Paldino [.NET/C# MVP]" wrote:

> Peter,
>
> That's assuming that the data table is only being accessed in that
> method, and that method is the only one that is being called from other
> threads.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "Peter Bromberg [C# MVP]" <(E-Mail Removed)> wrote in message
> news:8D45ECF4-21C0-41A6-AF22-(E-Mail Removed)...
> > Ryan,
> > It is not clearly evident from your post how you could have gotten the
> > exception you report, but if you want to ensure thread safety, you can use
> > the lock statement on a local object variable:
> >
> > object locker = new object();
> > lock (locker)
> > {
> >
> > // your code here
> >
> >
> > }
> >
> > --Peter
> > --
> > Co-founder, Eggheadcafe.com developer portal:
> > http://www.eggheadcafe.com
> > UnBlog:
> > http://petesbloggerama.blogspot.com
> >
> >
> >
> >
> > "Ryan Liu" wrote:
> >
> >> Hi,
> >>
> >> In the .NET Framework SDK documentation, I can see DataRow.AcceptChanges
> >> method will throw RowNotInTableException exeception.
> >>
> >> And in DataTable.AcceptChanges(), the documentation does not mention it
> >> will
> >> throw any exception, but in my code (multi-thread), I see it throw
> >> exceptions at two situations:
> >>
> >> dr = this.currentQuotaUserDt.NewRow();
> >>
> >> this.currentQuotaUserDt.Rows.Add(dr);
> >>
> >> this.currentQuotaUserDt.AcceptChanges();
> >>
> >> Last line throw exception : Cannot perform this operation on a row not
> >> in
> >> the table.
> >>
> >> (Code is more complex than what list here and it is multi-thread
> >> enviorment.)
> >>
> >> ==============
> >> this.currentDr.Delete();
> >> this.currentDr.Table.AcceptChanges();
> >>
> >> The last line will throw:
> >> System.Data.RowNotInTableException: Cannot perform this operation on a
> >> row
> >> not in the table.
> >> at System.Data.DataTable.SetOldRecord(DataRow row, Int32
> >> proposedRecord)
> >> at System.Data.DataTable.CommitRow(DataRow row)
> >> at System.Data.DataTable.AcceptChanges()
> >>
> >>
> >>
> >> Thanks,
> >> Ryan
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataTable NewRecord, Delete, then AcceptChanges Craig Buchanan Microsoft VB .NET 0 21st Jul 2008 01:10 PM
A DataTable-bound DataGridView throws a low-level Exception whencolumns are sorted, removed and re-added to the underlying DataTable. Sath123 Microsoft Dot NET Framework Forms 1 12th May 2008 12:51 PM
Make Changes to DataTable, Doesn't commit to database, used acceptchanges() Mojax Microsoft ADO .NET 0 14th Apr 2006 05:12 AM
empty DataTable fields throw exception Edward Bills Microsoft ADO .NET 2 25th Jun 2004 03:46 PM
DataTable.AcceptChanges Allan Bredahl Microsoft Dot NET 8 14th Jan 2004 04:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 AM.