Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 list views: PokedexList, PkmFormList, and PkmAbList. PokedexList is populated from a database when the form loads. When the user selects a pokemon from PokedexList it populates PkmFormList with all the forms of the selected pokemon. When a form is selected from PkmFormList, PkmAbList is populated with the abilities of the selected pokemon form. This is what's expected and this is what it does. However, it only does it for the first item selected.

Example: I select "Absol" from PokedexList, that populates PkmFormList with Absol's two forms, "Normal" and "Mega." However, if I then decide that, instead of "Absol", I wanted "Deoxys", I would expect PkmFormList to be populated with "Normal", "Attack", "Defense", and "Speed". Instead, it pops an error that says "InvalidArgument=Value of '0' is not valid for 'index'."

Private Sub PokedexList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PokedexList.SelectedIndexChanged

    'shows the selected pokemon as part of your party
    If Pkm1Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm1Txt.Text = item.Text
        Next

    ElseIf Pkm2Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm2Txt.Text = item.Text
        Next

    ElseIf Pkm3Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm3Txt.Text = item.Text
        Next

    ElseIf Pkm4Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm4Txt.Text = item.Text
        Next

    ElseIf Pkm5Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm5Txt.Text = item.Text
        Next

    ElseIf Pkm6Select.Checked = True Then
        For Each item As ListViewItem In PokedexList.SelectedItems
            Pkm6Txt.Text = item.Text
        Next
    End If

    'pulls a list of forms for the selected pokemon
    Dim SqlQry As String = "SELECT [Pokemon_forms].[pokemon_id], [Pokemon].[id], [Pokemon].[identifier], [Pokemon_forms].[form_identifier] " &
        "FROM [Pokemon], [Pokemon_forms] WHERE (([Pokemon].[id]=[Pokemon_forms].[pokemon_id])) AND [Pokemon].[identifier] = ?;"

    DbCommand = New OleDbCommand(SqlQry, DbConnection)
    Dim IdentifierParam As New OleDbParameter("?", OleDbType.VarChar)
    DbCommand.Parameters.Add(IdentifierParam)
    IdentifierParam.Value = PokedexList.SelectedItems(0).Text

    DbConnection.Open()

    Dim da As OleDbDataAdapter = New OleDbDataAdapter(DbCommand)

    Dim ds As DataSet = New DataSet

    da.Fill(ds, "Pokemon_forms")

    Dim dt As DataTable = ds.Tables("Pokemon_forms")

    Dim row As DataRow

    For Each row In dt.Rows
        Dim FormIdentifier As String = row("form_identifier")

        FormIdentifier = StrConv(FormIdentifier, vbProperCase)

        PkmFormList.Items.Add(FormIdentifier)
    Next

    DbConnection.Close()

End Sub


Any ideas how to fix this?
Posted
Updated 11-Nov-15 1:04am
v2

VB
'this is where it throws the error
IdentifierParam.Value = PokedexList.SelectedItems(0).Text

Then it is reasonable to assume that PokedexList.SelectedItems does not contain any items. Use your debugger to find out why.
 
Share this answer
 
Comments
KitsunePhoenix 11-Nov-15 6:19am    
I tried. I don't come here until i've exhausted every possibility, all the debugger will tell me is that there's nothing there, and I can't figure out why. It works for the first item I select, but when I try to change the selection it seems to be running the code before the new item is selected.
Richard MacCutchan 11-Nov-15 6:36am    
Then you need to show us the code that is causing the problem. We cannot see what happens when the PokedexList selection changes.
KitsunePhoenix 11-Nov-15 7:03am    
the code I posted in the question is where the problem is. The only other thing the code does when PokedexList is changed is to post the new pokemons name in a textbox. I'll add that just in case it helps but i can't see how it would.
Richard MacCutchan 11-Nov-15 7:25am    
Some of that code does not make sense. You are using a For Each construct to set a single item, so you will only get the last value. I suggest when you get the SelectedItems property, you first check that it actually contains some items.
KitsunePhoenix 11-Nov-15 8:31am    
it worked! All I had to do was put the entire query (i.e. the data adapter, data table etc.) into an If statement that activates when PokedexList.SelectedItems.Count > 0. Thank you so much! As for th efor each constructs setting the text boxes, there is only one selected item allowed so there is only one item to set. It's actually something I forgot to change, I only just learned to pull the text from a single listview item.
"InvalidArgument=Value of '0' is not valid for 'index'."

Did you try with
VB
Option Base 0

?
 
Share this answer
 
Comments
KitsunePhoenix 11-Nov-15 3:17am    
'Option' must be followed by 'Compare', 'Explicit', 'Infer', or 'Strict'.
Richard Deeming 11-Nov-15 9:55am    
That's a VB6 option that was removed in VB.NET v1. :)
phil.o 12-Nov-15 1:04am    
Oups :) My memory betrayed me, then. Thanks for clarification :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900