Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox list insert index less than size... vb.net

Status
Not open for further replies.

lisat76

Programmer
Sep 25, 2007
89
US
I have tried several different way to insert checked valuse into a db I keep getting
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index "

The sub will insert the first checked value but will throw the above error if there is more than one box checked even if the error is thrown it will still insert the first checked value.


Here are 2 examples of code i tried

Code:
Protected Sub addPlayertoTeam(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsPlayer.Click

        Dim ckbox As ListItem

        For Each ckbox In ckplayers.Items
            If ckbox.Selected Then
                Dim lbplayerval As Integer = ckbox.Value
                Dim TeamId As Integer = ddlteams.SelectedValue
                 sdsinplayers.InsertParameters(0).DefaultValue = TeamId
                sdsinplayers.InsertParameters(1).DefaultValue = lbplayerval
                sdsinplayers.InsertParameters(2).DefaultValue = username
                sdsinplayers.Insert()
                sdsinplayers.InsertParameters.Clear()

            End If

        Next
        ckplayers.DataBind()
        gviewPlTeams.DataBind()
    End Sub

Code:
    Protected Sub addPlayertoTeam(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsPlayer.Click
        Dim ctr As Integer
       
        For ctr = 0 To ckplayers.Items.Count - 1
                     
            If ckplayers.Items(ctr).Selected Then
                Dim lbplayerval As Integer = ckplayers.SelectedValue
                Dim TeamId As Integer = ddlteams.SelectedValue
             
                sdsinplayers.InsertParameters(0).DefaultValue = TeamId
                sdsinplayers.InsertParameters(1).DefaultValue = lbplayerval
                sdsinplayers.InsertParameters(2).DefaultValue = username
                sdsinplayers.Insert()
                sdsinplayers.InsertParameters.Clear()

            End If

        Next
        ckplayers.DataBind()
        gviewPlTeams.DataBind()
    End Sub
i get the same error regardless of the code
 
I dont think you can do a For Each on a sqldatasource.


the link has some examples saying you need to extend the OnInserting event handler

This link has a real similar situation

im not a fan of posting lots of external links, but they better describe and possibly resolve your issue.
 
Code:
sdsinplayers.InsertParameters.Clear()
this removes all parameters from the collection. just delete this line and you should be good.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
this removes all parameters from the collection. just delete this line and you should be good. "

If i remove the parameters.clear then it inserts the first selected value x number of times that a box is checked.
So i i check 3 different boxes with different value it
will insert the value of the first checked box 3 times.
 
nevermind i got it i changed this
Code:
 Dim lbplayerval As Integer = ckplayers.SelectedValue

to this
Code:
  Dim lbplayerval As Integer = ckplayers.Items(ctr).Value

and deleted the insertparam.clear

worked like a charm thanks...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top