473,769 Members | 5,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi,

In the .NET Framework SDK documentation, I can see DataRow.AcceptC hanges
method will throw RowNotInTableEx ception exeception.

And in DataTable.Accep tChanges(), 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.currentQuo taUserDt.NewRow ();

this.currentQuo taUserDt.Rows.A dd(dr);

this.currentQuo taUserDt.Accept Changes();

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.AcceptCha nges();

The last line will throw:
System.Data.Row NotInTableExcep tion: Cannot perform this operation on a row
not in the table.
at System.Data.Dat aTable.SetOldRe cord(DataRow row, Int32 proposedRecord)
at System.Data.Dat aTable.CommitRo w(DataRow row)
at System.Data.Dat aTable.AcceptCh anges()

Thanks,
Ryan
Jun 7 '06 #1
3 14790
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.AcceptC hanges
method will throw RowNotInTableEx ception exeception.

And in DataTable.Accep tChanges(), 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.currentQuo taUserDt.NewRow ();

this.currentQuo taUserDt.Rows.A dd(dr);

this.currentQuo taUserDt.Accept Changes();

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.AcceptCha nges();

The last line will throw:
System.Data.Row NotInTableExcep tion: Cannot perform this operation on a row
not in the table.
at System.Data.Dat aTable.SetOldRe cord(DataRow row, Int32 proposedRecord)
at System.Data.Dat aTable.CommitRo w(DataRow row)
at System.Data.Dat aTable.AcceptCh anges()

Thanks,
Ryan

Jun 7 '06 #2
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]
- mv*@spam.guard. caspershouse.co m

"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > wrote in message
news:8D******** *************** ***********@mic rosoft.com...
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.AcceptC hanges
method will throw RowNotInTableEx ception exeception.

And in DataTable.Accep tChanges(), 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.currentQuo taUserDt.NewRow ();

this.currentQuo taUserDt.Rows.A dd(dr);

this.currentQuo taUserDt.Accept Changes();

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.AcceptCha nges();

The last line will throw:
System.Data.Row NotInTableExcep tion: Cannot perform this operation on a
row
not in the table.
at System.Data.Dat aTable.SetOldRe cord(DataRow row, Int32
proposedRecord)
at System.Data.Dat aTable.CommitRo w(DataRow row)
at System.Data.Dat aTable.AcceptCh anges()

Thanks,
Ryan

Jun 7 '06 #3
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]
- mv*@spam.guard. caspershouse.co m

"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > wrote in message
news:8D******** *************** ***********@mic rosoft.com...
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.AcceptC hanges
method will throw RowNotInTableEx ception exeception.

And in DataTable.Accep tChanges(), 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.currentQuo taUserDt.NewRow ();

this.currentQuo taUserDt.Rows.A dd(dr);

this.currentQuo taUserDt.Accept Changes();

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.AcceptCha nges();

The last line will throw:
System.Data.Row NotInTableExcep tion: Cannot perform this operation on a
row
not in the table.
at System.Data.Dat aTable.SetOldRe cord(DataRow row, Int32
proposedRecord)
at System.Data.Dat aTable.CommitRo w(DataRow row)
at System.Data.Dat aTable.AcceptCh anges()

Thanks,
Ryan


Jun 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1844
by: Sasha | last post by:
Hi, I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes and all... My idea is to have private Guid state field in the collection, and every time something is inserted or deleted from the collection, I will just change the guid. Enumerator will just have to compare the guid received in the begging to the current one. If they are different, the...
6
1816
by: chris | last post by:
Hi, I have a table in the database. I have a method which will return a table with the same structure as the DB one and with all data I need. What I want to do is to remove all data in the DB table and reload it with the table I created. I tried to use SqlDBAdaptor and it seems I cannot do something like ds.Tables=myTable
13
5058
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
2974
by: Mike | last post by:
I have an ASP.NET/VB app that updates values in a DataTable over the course of about 3 different pages. On the way out of the first of these pages, I explicitly build the DataTable from values in a DataGrid, and set the PrimaryKey of the DataTable to be the first cell in the grid (which is a UserID value). I then store the DataTable in a session object, from which it is retrieved for subsequent pages. All this seems to be working fine. ...
6
1702
by: Danny Ni | last post by:
Hi, If I want to programatically add rows to a DataTable, do I call AcceptChanges per row? Or do I call AcceptChanges after all rows added? TIA
1
4246
by: Ryan Liu | last post by:
Hi, I thought the following code is tedious but pretty safe, unexpected this.dataTable.DefaultView itself throw an System.NullReferenceException? if(this.dataTable!= null && this.dataTable.DefaultView != null && this.dataTable.DefaultView.Count 0)
4
6221
by: jayesah | last post by:
Hi All, I am writting a Thread class with using pthread library. I have some problem in saving thread function type and argument type. How to make Thread class generic ? /* This is my global Function */ template < class FunType, class ArgType> Thread makeThread(Funtype fun, ArgType arg)
2
2500
by: =?Utf-8?B?Sm9iIExvdA==?= | last post by:
How can I reconcile changes between two data table and create a subset of those changes. I have two data tables with same schema which gets populated from two different xml files. I want to get hold of missing & changed rows in first table from second table. Tables have ID as primary key. Thanks
6
3141
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.