שתף באמצעות


InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index

Question

Saturday, March 23, 2013 12:59 PM

Hi All,

I'm new to MySQL, I have a program (VB.NET 2010) wherein I have a text box and when I press enter it will query the DB and display results in a DataGridView. But I am getting an error after pressing the Enter key :

InvalidArgument=Value of '-1' is not valid for 'index'.
Parameter name: index

Here's the code:

Public Sub SearchByName(ByVal name As String)

        dgvInfo.ClearSelection()
        lstNames.ClearSelected()

        Dim dgvRow As DataGridViewRow = _
            (From row As DataGridViewRow In dgvInfo.Rows
            Where row.Cells("colName").Value.ToString().ToUpper().Contains(name.ToUpper())
            Select row).FirstOrDefault()

        If Not dgvRow Is Nothing Then
            dgvRow.Selected = True
            dgvInfo.FirstDisplayedScrollingRowIndex = dgvRow.Index
            dgvInfo.PerformLayout()
        End If

        Dim index = lstNames.FindString(name)
        lstNames.SetSelected(index, True)
    End Sub

I'm a newbie, any help will be appreciated

All replies (2)

Saturday, March 23, 2013 1:02 PM ✅Answered

Dim index = lstNames.FindString(name)If index = -1, this means the string contained by "name" variable isn't found in lstNames. So plz make sure that your lstNames must include the string of "name

If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats


Saturday, March 23, 2013 9:03 PM ✅Answered

You have an inconsistency between the search used for the row, and the seach used for the list:

        Where row.Cells("colName").Value.ToString().ToUpper().Contains(name.ToUpper())
        Dim index = lstNames.FindString(name)

That creates the possibility that , even though you have a valid row, the search within the list will fail.   You should alter the search in the list so that it matches the search for the row. But in any case your code to set the selection should be inside the test for a valid row.

If you insert a breakpoint in the last row of that sub and run  your code to the breakpoint you can examine name and the list to see the reason for the seach in the list failing.