Loop the loop...

Discussion in 'ASP .Net' started by =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005.

  1. 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
    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?VGltOjouLg==?=

    Iain Norman Guest

    On 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
    Iain Norman, Feb 16, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?VGltOjouLg==?=

    Karl Seguin Guest

    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:...
    > 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
    >
    >
    >
    >
    Karl Seguin, Feb 16, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,212
    Tim Slattery
    Mar 30, 2005
  2. -
    Replies:
    12
    Views:
    675
    Remon van Vliet
    Jun 15, 2005
  3. Byte
    Replies:
    4
    Views:
    400
  4. Daniel Pitts
    Replies:
    14
    Views:
    1,212
    Patricia Shanahan
    Dec 23, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    348
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page