'Solution for this error "InvalidArgument=Value of '0' is not valid for 'index'."

The 'cause of error is when I trigger the event, I tried to disable it to prevent the error. But, upon enabling it again. When, the event is triggered the error would show up again.

I Googled some solutions like checking if items exist in the ListView

If ListView2.Items.Count > 0 Then

I also, found some code on handling the event. But, It seems to do nothing.

AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged

I thought removing the items will remove the error. But, It didn't work.

Here is the code:

For i = ListView2.Items.Count - 1 To 0 Step -1
  ListView2.Items.Remove(ListView2.Items(i))
Next i

Below are the full codes for the event:

Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
    If ListView2.Items.Count > 0 Then
        TransactionID.Text = ListView2.SelectedItems(0).Text
        Label6.Left -= 190
        Label7.Left -= 190
        GroupBox1.Left -= 190
        ListView2.Left -= 190

        Button2.Visible = True
        ListView1.Visible = True
        GroupBox2.Visible = True
        Label4.Visible = True
        Label5.Visible = True
        ListView2.Enabled = False
        AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged
    End If
End Sub

I found out that the cause of the error is that I'm trying to select the ListView2 but, I no longer have access to it, there are no items in the Listview or I haven't selected it. It's just strange the I made sure the Listview has items and the Listview is enabled.

So yeah... What could possibly the solution for this?

EDIT: ADDED THE CODE FOR BINDING THE LISTVIEW

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If ComboBox1.Text = "" Then
        MsgBox("Please specify status of transaction!")
        ComboBox1.Focus()
    End If
    Dim conn As MySqlConnection
    conn = New MySqlConnection()
    Label6.Text = 0

    time = dateTo.Text
    format = "yyyy-MM-dd"
    outTo = (time.ToString(format))
    outTo = outTo & " " & "23:59:59"

    time = dateFrom.Text
    format = "yyyy-MM-dd"
    outFrom = (time.ToString(format))
    outFrom = outFrom & " " & "00:00:00"

    conn.ConnectionString = "server=localhost;user id=root;password=zhakige;database=singin"
    Dim strSQL = "SELECT transaction_id, transaction_status, transaction_staffusername ,transaction_date, transaction_totalprice FROM `transaction` WHERE transaction_date BETWEEN '" & outFrom & "' AND '" & outTo & "' AND transaction_status = '" & ComboBox1.Text & "';"
    conn.Open()
    Dim cmd = New MySqlCommand(strSQL, conn)
    Dim dr = cmd.ExecuteReader()
    ListView2.Items.Clear()

    Do While dr.Read()
        a = (dr.Item("transaction_id").ToString())
        b = (dr.Item("transaction_status").ToString())
        c = (dr.Item("transaction_staffusername").ToString())
        time = (dr.Item("transaction_date"))
        format = "yyyy-MM-dd"
        output = (time.ToString(format))
        d = output
        e2 = (dr.Item("transaction_totalprice").ToString())


        Dim lv As ListViewItem = ListView2.Items.Add(a)
        lv.SubItems.Add(b)
        lv.SubItems.Add(c)
        lv.SubItems.Add(d)
        lv.SubItems.Add(e2)
        Label6.Text += Val(e2)


    Loop
    If ListView2.Items.Count <= 0 Then
        MsgBox("No record found for specified options")
    End If

    dr.Close()
    cmd.Dispose()
    conn.Close()
End Sub


Solution 1:[1]

MSDN says

A ListView.SelectedListViewItemCollection that contains the items that are selected in the control. If no items are currently selected, an empty ListView.SelectedListViewItemCollection is returned.

So if you don't have any SelectedItems you can't use index 0 to read some value. Change your code to

If ListView2.SelectedItems.Count > 0 Then
   ....

Solution 2:[2]

Change this code

For i = ListView2.Items.Count - 1 To 0 Step -1
  ListView2.Items.Remove(ListView2.Items(i))
Next i

to

If ListView2.Items.Count > 0 Then
  For i = ListView2.Items.Count - 1 To 0 Step - 1
    ListView2.Items.Remove(ListView2.Items(i))
  Next i
End If

Update

in this code

Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
  If ListView2.Items.Count > 0 Then
    TransactionID.Text = ListView2.SelectedItems(0).Text
    Label6.Left -= 190
    Label7.Left -= 190
    GroupBox1.Left -= 190
    ListView2.Left -= 190

    Button2.Visible = True
    ListView1.Visible = True
    GroupBox2.Visible = True
    Label4.Visible = True
    Label5.Visible = True
    ListView2.Enabled = False
    AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged
  End If
End Sub

the line that says

    ListView2.Enabled = False

disables the ListView2, so that should explain WHY and WHERE the ListView2 is being disabled

Also... This line

AddHandler ListView2.SelectedIndexChanged, AddressOf ListView2_SelectedIndexChanged

attempts to assign an event handler to the same routine it is declared in!

Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged

So chances are it probably does nothing new at all.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve
Solution 2