Exception: Index was outside the bounds of the array

G

Guest

Hi,

I'm dealing with a master-detail relationship. A datagrid bound to the
detail data. When navigating through the records (next record:
Me.BindingContext(mDv1).Position += 1), I notice a delay of about 2 sec's if
the detail datagrid is empty (no records to show for this master-record). The
delay happens only once per application run!! After exception debugging a
found that when this happens a "first chance exception of type
'System.IndexOutOfRangeException' is thrown. Who knows how to deal with/solve
this situation?

Regards Coen
 
G

Guest

Hi Cor, thanks for your response.

Unfortunately this is not the case. The delay happens whenever the related
detail table has no related rows to show. This can happen at master row nr 5
of 100, or whatever... Any case the master table is not even near his last
record. The most important issue is that it always happens only once per
application-run, and only if the related detail table has for the current
master record no corresponding detail rows.

Again, any help is welcome.
Regards Coen.
 
C

Cor Ligthert

Coen,

After that an exception is thrown the "same" next exception takes only some
milliseconds so you will probably not see that however it is probably much
more in your program.

From the information I have now I cannot see what it can be, however it
should than be somewhere in your related table in the way you describe it.

Cor
 
C

Cor Ligthert

However I agree that there should than be an exception, when you did not as
some people do put an empty Try Catch block around it

Try
do something
Catch
End Try

What is of course the worsest thing in programming so I assume you did not
do that.

Cor
 
G

Guest

Cor,

I'm not using empty Try Catch blocks. But I have found the cause of this
exception. The exception only occurs when the related datagrid's READONLY
property is put to true. Navigating to the next (master) record without any
related detail records to show (in the datagrid), causes this
IndexOutOfRangeException. Of course I can work around this like:

grdContacts.ReadOnly = False
Me.BindingContext(mDv1).Position += 1
grdContacts.ReadOnly = True

This works fine, I have tested this successfull. Still I'm supprised this
happens. I think, this was not necessary in VS2002+Framework 1.0. I could
check this next wednesday. I wonder if this is logic and necessary, I would
like your opinion of this.

Regards Coen
 
C

Cor Ligthert

Coen,

I tested this in a program I have, however I see in the related datagrid a
nice header with no rows.

This datagrid is with columnstyles by the way.

Cor
 
G

Guest

Cor, I suppose you turned on "catching the IndexOutOfRangeException", via the
Debug menu, Exceptions, CLR exceptions, System,
System.IndexOutOfRangeException. --> Breaking into the debugger? Otherwise
you'll see only a two sec's delay (only once), but no execute interuption.

I downsized my project, also using a dataset without a dataview, but the
exception is still there, however the remaining code is very basic. I'll
paste it underneath, so maybe you can have a look at it? The form contains
four textboxes (bound to the master table), four navigation buttons, and one
datagrid (detail-rows). A connection is made to the Northwind database
(please modify the connectionstring).

Maybe you see a difference with your testcode?
Regards Coen

--------------------- Start Code: -------------------
Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
Inherits System.Windows.Forms.Form

Dim myConnection As SqlConnection
Dim mDs As New DataSet
Dim mDa1 As SqlDataAdapter
Dim mDa2 As SqlDataAdapter

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

myConnection = New
SqlConnection("server=(local)\INSTANCE01;Trusted_Connection=yes;database=northwind")
mDa1 = New SqlDataAdapter("SELECT * FROM Customers", myConnection)
mDa2 = New SqlDataAdapter("SELECT * FROM orders", myConnection)

'Fill dataset
mDa1.Fill(mDs, "Customers")
mDa2.Fill(mDs, "Orders")

'Define relation
mDs.Relations.Add(New DataRelation( _
relationName:="relCustomersOrders", _

parentColumn:=mDs.Tables("Customers").Columns("CustomerId"), _

childColumn:=mDs.Tables("Orders").Columns("CustomerId"), _
createconstraints:=True))

'Eventhandlers navigatie
Call AddHandlers()

'Some textboxes databinding to the master table
txtCustomerId.DataBindings.Add("Text", mDs.Tables("Customers"),
"CustomerId")
txtCompanyname.DataBindings.Add("Text", mDs.Tables("Customers"),
"CompanyName")
txtContactname.DataBindings.Add("Text", mDs.Tables("Customers"),
"ContactName")
txtPostalcode.DataBindings.Add("Text", mDs.Tables("Customers"),
"PostalCode")

'Bind the datagrid to the detail table via the datarelation
grdOrders.DataSource = mDs.Tables("Customers")
grdOrders.DataMember =
mDs.Tables("Customers").ChildRelations("relCustomersOrders").ToString
grdOrders.ReadOnly = True 'Set the datagrid to READONLY

End Sub

Private Sub AddHandlers()
'Eventhandler navigate-buttons
AddHandler btnFirst.Click, AddressOf Me.Navigate
AddHandler btnNext.Click, AddressOf Me.Navigate
AddHandler btnPrevious.Click, AddressOf Me.Navigate
AddHandler btnLast.Click, AddressOf Me.Navigate
End Sub

Private Sub Navigate(ByVal sender As System.Object, ByVal e As
System.EventArgs)

'grdOrders.ReadOnly = False '--> This line prevents the
IndexOutOfRangeException
With Me.BindingContext(mDs.Tables("Customers"))
Select Case sender.name
Case "btnFirst"
.Position = 0
Case "btnPrevious"
.Position -= 1
Case "btnNext"
.Position += 1
Case "btnLast"
.Position = .Count - 1
End Select
End With
'grdOrders.ReadOnly = True '<-- This line prevents the
IndexOutOfRangeException

End Sub
End Class
 
C

Cor Ligthert

Coen,

I tried it and no errors (it delays however no exception).

I assume that you have option strict not on, because this row should not be
accepted in my opinion
Select Case sender.name


It has to be
Select Case DirectCast(sender, Button).Name

However that should not give the exception.

Cor
 
G

Guest

Cor, you are right about the late binding, I have option strict put ON now.
But indeed this is of no influence in this exception matter.

If you are experiencing a delay, something must be happening there. I
suppose you also tried running while all the exceptions set to the "Break
into the debugger" option? Also in your project, something must be causing
the delay...

By the way, using
Dev. env. 7.1.3088
Framework: 1.1.4322 SP1

Coen
 
C

Cor Ligthert

Coen,

No I had not, so something strange is happening here under the blanket.

Tomorrow evening is a chat about debugging. When you are not there I (when I
dont forge) will ask that.

Cor
 
G

Guest

Hi Holger,

Thanks for your response. I indeed believe this is describing the same sort
of problem. The last post confirms the post of Alfred being a bug. I got the
same question you have: after confirming the bug-situation, has there been
delivered any hotfix? I don't know, I can't find one. Perhaps Microsoft's
people can react to this post? Please do.

Regards Coen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top