I have an error on one of my loops and am not completely sure why I keep
getting the following error message!
Insertion index was out of range. Must be non-negative and less than or
equal to size. Parameter name: index
Would appritiate any input... Thanks
..CODE
Private Sub PopulateDDLNoMobAccounts()
'assuming you have a button named ForLoop
Dim i As Integer
For i = 1 To 10
v_intNoMobilePhoneNumbers.Items.Insert(i, "i")
v_intNoMobilePhoneNumbers.Items.FindByText("i").Value = i
Next i
v_intNoMobilePhoneNumbers.DataBind()
v_intNoMobilePhoneNumbers.Items.Insert(0, "Select One")
v_intNoMobilePhoneNumbers.Items.FindByText("Select One").Value = 0
'insert don't create a value, but we need a value during defaults
v_intNoMobilePhoneNumbers.SelectedIndex = 0
End SubOn Wed, 16 Feb 2005 03:23:06 -0800, "Tim::.." <myatix_at_hotmail.com>
wrote:
>I have an error on one of my loops and am not completely sure why I keep
>getting the following error message!
>Insertion index was out of range. Must be non-negative and less than or
>equal to size. Parameter name: index
>Would appritiate any input... Thanks
>
>..CODE
>Private Sub PopulateDDLNoMobAccounts()
> 'assuming you have a button named ForLoop
> Dim i As Integer
> For i = 1 To 10
> v_intNoMobilePhoneNumbers.Items.Insert(i, "i")
> v_intNoMobilePhoneNumbers.Items.FindByText("i").Value = i
> Next i
> v_intNoMobilePhoneNumbers.DataBind()
> v_intNoMobilePhoneNumbers.Items.Insert(0, "Select One")
> v_intNoMobilePhoneNumbers.Items.FindByText("Select One").Value = 0
>'insert don't create a value, but we need a value during defaults
> v_intNoMobilePhoneNumbers.SelectedIndex = 0
> End Sub
>
>
Is it zero indexed?
I
Iain Norman | http://www.eliteforum.org
Tim:
you are trying to insert in position 1 before inserting anything in position
0, try this for simpler, cleaner code:
For i As Integer = 1 To 10
v_intNoMobilePhoneNumbers.Items.Add(New ListItem(i.ToString(),
i.ToString()))
Next i
v_intNoMobilePhoneNumbers.Items.Insert(0, New ListItem("Select One", "0"))
v_intNoMobilePhoneNumbers.SelectedIndex = 0
(a) notice that I don't use insert...insert is when you want to put
something in a specific position, at first we just want to add the items
(b) note that I still use insert to add the "Select One" as the very first
item
(c) I got rid of the DataBind() this does nothing unless you are
databinding except eat up cycles
(d) I add/insert new ListItems which allows me to specify both the text and
the value in a single line
(e) I turned on Option Strict which means I can't just assign i to something
which expects a string - I must ToString() it (always use option strict)
Karl
MY ASP.Net tutorials
http://www.openmymind.net/
"Tim::.." <myatix_at_hotmail.com> wrote in message
news:2B796BF2-B9D2-4F38-ADD5-DE54C6115AEF@.microsoft.com...
> I have an error on one of my loops and am not completely sure why I keep
> getting the following error message!
> Insertion index was out of range. Must be non-negative and less than or
> equal to size. Parameter name: index
> Would appritiate any input... Thanks
>
> ..CODE
> Private Sub PopulateDDLNoMobAccounts()
> 'assuming you have a button named ForLoop
> Dim i As Integer
> For i = 1 To 10
> v_intNoMobilePhoneNumbers.Items.Insert(i, "i")
> v_intNoMobilePhoneNumbers.Items.FindByText("i").Value = i
> Next i
> v_intNoMobilePhoneNumbers.DataBind()
> v_intNoMobilePhoneNumbers.Items.Insert(0, "Select One")
> v_intNoMobilePhoneNumbers.Items.FindByText("Select One").Value = 0
> 'insert don't create a value, but we need a value during defaults
> v_intNoMobilePhoneNumbers.SelectedIndex = 0
> End Sub
>
>
Monday, March 26, 2012
Loop the loop...
Labels:
asp,
error,
following,
index,
keepgetting,
loop,
loops,
messageinsertion,
net,
range
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment