Urgent: DataTable.Clear problem..

A

Adrian Parker

We've suddenly started getting a problem with a call to clear the contents
of a DataTable. This is on a live customer site that's been working fine
until yesterday. As far as we know they've not changed or updated the
server in any way.

"There is no row at position 42"

at System.Data.RBTree`1.GetNodeByIndex(int32 userIndex)
at System.Data.DataTable.Clear(Boolean clearAll)
at system.Data.DataTable.Clear()

Public Overrides Sub GetTable(ByRef aTbl As DataTable, ByVal aSQL As String)
If aTbl Is Nothing Then
aTbl = New DataTable
Else
aTbl.Clear()
End If
' code to retrieve data
End Sub



Any ideas ?
 
K

Karl Seguin [MVP]

Seems like a multithreading problem.

What's the scope of the DataTable in question? Is it stored in the
Application? In a shared/static field? In the Session object, a local var of
the calling function? The Cache?

My guess is that it's shared accross multiple users via the Cache,
Application or a static/shared field....and you're getting 1 request
modifying the table at the same time as another one is trying to clear it.

Whenever you deal with resources across multiple threads, you need to be
careful to syncrhonize any volatile operation..

Specifically, this is code within Clear:

for (int num1 = 0; num1 < this.rowCollection.Count; num1++)
{
DataRow row1 = this.rowCollection[num1];
row1.oldRecord = -1;
row1.newRecord = -1;
//do more stuff with row1
}


if this.rowCollection.Count returns 100....
it starts to loop..by the time it reachs the 99, another thread might have
deleted row 99, and then you're in the mess you're in :)

Simply placing locks around the volatile code is the simplest solution,
though I would recommend a more thorough examination..

karl
 
W

Walter Wang [MSFT]

Hi Adrian,

Based on my experience, this is most likely caused by the underlying
DataTable's content is being changed by other thread while Clear() is
executing. Is it possible that other thread may be referencing the
DataTable too?

Also, in your specific scenario, wouldn't it be ok to always create a new
DataTable in your GetTable() method?

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Adrian Parker

Aha, code has the datatable varaible created as Public Shared for some
reason.

Thanks for hte hint :)

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message | Seems like a multithreading problem.
|
| What's the scope of the DataTable in question? Is it stored in the
| Application? In a shared/static field? In the Session object, a local var
of
| the calling function? The Cache?
|
| My guess is that it's shared accross multiple users via the Cache,
| Application or a static/shared field....and you're getting 1 request
| modifying the table at the same time as another one is trying to clear it.
|
| Whenever you deal with resources across multiple threads, you need to be
| careful to syncrhonize any volatile operation..
|
| Specifically, this is code within Clear:
|
| for (int num1 = 0; num1 < this.rowCollection.Count; num1++)
| {
| DataRow row1 = this.rowCollection[num1];
| row1.oldRecord = -1;
| row1.newRecord = -1;
| //do more stuff with row1
| }
|
|
| if this.rowCollection.Count returns 100....
| it starts to loop..by the time it reachs the 99, another thread might have
| deleted row 99, and then you're in the mess you're in :)
|
| Simply placing locks around the volatile code is the simplest solution,
| though I would recommend a more thorough examination..
|
| karl
|
| --
| http://www.openmymind.net/
| http://www.fuelindustries.com/
|
|
| | > We've suddenly started getting a problem with a call to clear the
contents
| > of a DataTable. This is on a live customer site that's been working
fine
| > until yesterday. As far as we know they've not changed or updated the
| > server in any way.
| >
| > "There is no row at position 42"
| >
| > at System.Data.RBTree`1.GetNodeByIndex(int32 userIndex)
| > at System.Data.DataTable.Clear(Boolean clearAll)
| > at system.Data.DataTable.Clear()
| >
| > Public Overrides Sub GetTable(ByRef aTbl As DataTable, ByVal aSQL As
| > String)
| > If aTbl Is Nothing Then
| > aTbl = New DataTable
| > Else
| > aTbl.Clear()
| > End If
| > ' code to retrieve data
| > End Sub
| >
| >
| >
| > Any ideas ?
| > --
| > Adrian Parker
| > Ingenuity At Work Ltd
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top