Index was out of range. Must be non-negative and less than the size of the collection Hi,

I've a form and the form is working fine also. But my problem is
Whenever I closed the form. I'm getting two errors.

1.Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

2.Arithmetic overflow error converting varchar to data type numeric.
The statement has been terminated.


Events are :
Private Sub btnFilClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFilClose.Click, btnBrowClose.Click

'This event is used to close the form

Try
Me.Cursor = Cursors.WaitCursor
Me.Close() ' I'm getting the error on this line
Me.Cursor = Cursors.Default
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message)
Finally
Me.Cursor = Cursors.Default
Me.Close()
End Try
End Sub

Private Sub forms_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'This event will be fired automatically whenever form will be closing

Try
Me.Cursor = Cursors.WaitCursor
If MessageBox.Show("Do you want to close this form", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

'If user press Yes then Form will be closed
Me.Cursor = Cursors.Default
e.Cancel = False
Else

'If User press No the Form will be not closed
e.Cancel = True
End If
Me.Cursor = Cursors.Default
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message)
End Try
End Sub

Please help me

Thanx in advance

Dani AI

Generated

Short answer: the exceptions are almost never thrown by Me.Close() itself — they come from code that runs during closing (FormClosing/FormClosed handlers, control events, data-binding validation or DB code). As noted, the close call just starts the shutdown; as pointed out, grid/header clicks can produce a -1 row index; and ' suggestion to guard against zero is on the right track but insufficient — test for a valid index range.

Troubleshooting steps (fast):

  • Reproduce under the debugger and enable "break on thrown exceptions" to get the full stack trace and exact line. The call stack shows which event/handler threw the error.
  • Temporarily comment out FormClosing/FormClosed handlers and any data-save logic to confirm the source.
  • Remove redundant/recursive close calls (calling Me.Close() in Finally while already closing can re-enter close logic).

Common fixes

  • Index error: always check bounds before using an index or row number (check >= 0 and < Count). Example guard for DataGridView cell events:

    If e.RowIndex >= 0 AndAlso e.RowIndex < myGrid.Rows.Count Then
        ' safe to access myGrid.Rows(e.RowIndex)
    End If
  • Arithmetic overflow: validate and parse user input before sending to SQL. Use Decimal.TryParse and parameterized queries with SqlDbType.Decimal, and set Precision and Scale on the parameter to match the SQL column. Example pattern:

    Dim v As Decimal
    If Decimal.TryParse(txtAmount.Text, v) Then
        Dim p As New SqlParameter("@amt", SqlDbType.Decimal)
        p.Precision = 18
        p.Scale = 2
        p.Value = v
        ' add parameter and execute command
    Else
        ' handle invalid input
    End If

Quick checklist: break on exceptions and read the stack trace; find the handler (cell click, row validating, save routine) and add index bounds checks; validate/tryparse numeric values and use typed SQL parameters; avoid duplicate Close calls; if data-binding commits on close, call BindingSource.CancelEdit() or skip save during closing until inputs are validated.

Recommended Answers

All 3 Replies

What is index? Where are you using it? Where are you using varchar to data type numeric? There is something wrong here not in your closing, it looks fine to me.

when header is clicked of data-grid it has -1 value in it. so make a check on it that if(i!=-1) then perform the operation else do nothing. Thnx

Have you tried re-programming the check so it will take a value other than zero?

If blah <> 0 then
' do something
else
' do something
end if
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.