Solved

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

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

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)…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

734 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