Solved

VB.NET Datatable "Index was out of range."

Posted on 2010-11-19
2
723 Views
Last Modified: 2012-05-10
I have VB.NET App with 2 threads.

Both threads read and update a Datatable.

When the App starts the structure and rows (20c, 100 r) are setup.

The Datatable is basically an object to hold information used by the program, the threads read/change this data while it runs.

THE PROBLEM:

Randomly get the error: "Index was out of range. Must be non-negative and less than the size of the collection."

When the code breaks on this error I can confirm using the "Immediate Window" that the reference is valid.

A typical reference is: dtData(10)("Name")

The Datatable schema, # rows, and # cols are not changing and I'm sure the Index is valid, and the ref Col exists.

Is there some other problem with 2 threads accessing the same Datatable??

Thanks!
0
Comment
Question by:cgraham2
2 Comments
 
LVL 4

Accepted Solution

by:
Andre412 earned 500 total points
Comment Utility
Are you balancing thread access ?

when using multi threaded datatable access you must balance access to the table.

lookup using the synclock

An example of how i use it on a dataset
declare this class wide
 
Private oSyncLockTables As New Object

Open in new window


then in functions that access the table
 If Monitor.TryEnter(oSyncLockTables, 500) Then
            Try
                If priDataSet.Tables.IndexOf(tblName.ToString) > -1 Then
                    Rtn = True
                Else
                    ''delete any reference to this table in the maintenance table (if there is one)
                    'TableIndex_DeleteRow(tblName.ToString)
                    Rtn = False
                End If
            Finally : Monitor.Exit(oSyncLockTables) : End Try
        End If

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Does the program need to show the datatable while it is being processed? If not, use separate datatables for both threads and merge them into one at the end.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wpf develop 5 29
Problem to go to Web page 2 46
System32Int Error 8 44
Round a string to two digits 12 23
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now