I've run this before without a problem, but now I'm getting a warning!

Okay, I've run this before without a problem, but now I'm getting a warning stating "InvalidArgument=Value of '1' is not valid for 'index'.
Parameter name: index"
.

Why would it begin giving me this error now after running it in debug several times without a problem before

Anyone, please help.




Answer this question

I've run this before without a problem, but now I'm getting a warning!

  • TomEire

    The other thing is, you're deleting a record then repopulating your list, so the "Count" is going to change.
  • manu.aretuseo

    I've tried this and it seemed to have no effect, in fact that was what I had there first, but thanks.

  • Steev

    no doubt that is correctly said, but if you still are unable to resolve the problem, post post us some code and the place the error happens so we can help you resolve it

  • Jim Perry

    Hi,

    The error message sounds like you are getting an IndexOutOfRange exception, or something similar. Chances are you are trying to access an array or collection with an invalid index.

    You might want to check that the collection you are accessing actually has data in it (look at the length field or Count property). Also, in VB.NET array indicies are 0-based, not 1-based, so the index of the first element in an array is 0, not 1.

    -Scott Wisniewski


  • wvvjoevvw

    I forgot to include the code, here it is. The line in yellow is what keeps poping up the warning during debugging.

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click

    'Check which item has been selected'

    Dim Count As Integer = Me.ListView1.Items.Count

    Dim AddFromZero As Integer = 0

    While AddFromZero <= Count

    If ListView1.Items(AddFromZero).Selected = True Then

    Dim SelectedDataRow = Me.Child_Information_DatabaseDataSet.Child_Information.Rows(AddFromZero)("FirstName") + " " + Me.Child_Information_DatabaseDataSet.Child_Information.Rows(AddFromZero)("LastName")

    Dim answer As DialogResult

    answer = MessageBox.Show("Are you sure you want to delete " + SelectedDataRow + " from the database ", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

    If answer = vbYes Then

    'Delete the selected database'

    Me.Child_Information_DatabaseDataSet.Child_Information.Rows(AddFromZero).Delete()

    'Update changes to the database'

    Me.Child_InformationTableAdapter.Update(Me.Child_Information_DatabaseDataSet.Child_Information)

    Me.Child_Information_DatabaseDataSet.Child_Information.AcceptChanges()

    'Clear the ListView and re-populate it with any updates of the database.'

    Me.ListView1.Clear()

    Child_Information_DatabaseDataSet.EnforceConstraints = False

    Me.Child_InformationTableAdapter.Fill(Me.Child_Information_DatabaseDataSet.Child_Information)

    For Each CurrentRow As DataRow In Me.Child_Information_DatabaseDataSet.Child_Information

    Me.ListView1.Items.Add(CurrentRow.Item("FirstName") + " " + (CurrentRow.Item("LastName")), 0)

    Next

    End If

    End If

    AddFromZero = AddFromZero + 1

    End While

    End Sub



  • japt

  • Lil endian

    Either subtract one from Count or change:

    While AddFromZero <= Count

    to

    While AddFromZero < Count

    The reason is because the list is Zero based.


  • hrubesh

    ahmedilyas wrote:
    no doubt that is correctly said, but if you still are unable to resolve the problem, post post us some code and the place the error happens so we can help you resolve it
    Cool, thanks, the code is below..

  • beefeater

    cool, thanks. this is what I added to my code and it works fine now. Thanks for spotting that....

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click

    'Check which item has been selected'

    Dim Count As Integer = Me.ListView1.Items.Count

    Dim AddFromZero As Integer = 0

    While AddFromZero < Count

    If ListView1.Items(AddFromZero).Selected = True Then

    Dim SelectedDataRow = Me.Child_Information_DatabaseDataSet.Child_Information.Rows(AddFromZero)("FirstName") + " " + Me.Child_Information_DatabaseDataSet.Child_Information.Rows(AddFromZero)("LastName")

    Dim answer As DialogResult

    answer = MessageBox.Show("Are you sure you want to delete " + SelectedDataRow + " from the database ", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

    If answer = vbYes Then

    'Delete the selected database'

    Me.Child_Information_DatabaseDataSet.Child_Information.Rows(AddFromZero).Delete()

    'Update changes to the database'

    Me.Child_InformationTableAdapter.Update(Me.Child_Information_DatabaseDataSet.Child_Information)

    Me.Child_Information_DatabaseDataSet.Child_Information.AcceptChanges()

    'Clear the ListView and re-populate it with any updates of the database.'

    Me.ListView1.Clear()

    Child_Information_DatabaseDataSet.EnforceConstraints = False

    Me.Child_InformationTableAdapter.Fill(Me.Child_Information_DatabaseDataSet.Child_Information)

    For Each CurrentRow As DataRow In Me.Child_Information_DatabaseDataSet.Child_Information

    Me.ListView1.Items.Add(CurrentRow.Item("FirstName") + " " + (CurrentRow.Item("LastName")), 0)

    Next

    End If

    End If

    Count = Me.ListView1.Items.Count

    AddFromZero = AddFromZero + 1

    End While

    End Sub



  • I've run this before without a problem, but now I'm getting a warning!