Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-19
2
Medium Priority
?
775 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 2000 total points
ID: 34176074
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
ID: 34179694
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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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