Solved

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

Posted on 2010-11-19
2
758 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 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