Solved

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

Posted on 2010-11-19
2
763 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
Andre412 earned 500 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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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