ASP.NET Newbie: asp:ListBox object error.."Index was out of range". HELP!

OK, Im stumped.


First off: I'm Green. As green as the day is long.  But i'm workin it! nuf said?

Now the problem:
===========
I am trying to work through an assignment in the book Teach Yourself ASP.NET in 21 Days (yeah right) and  i have followed (most of) the code to the letter (i did, change variable names as I am trying to do it from memory/lessons).

When I run the code (written in VB [visual studio.net/WinXP Pro]), i get an error [see code THEN error below]. However, see some other "concerns":

1.) In the "aspx.vb" file [see code below]  their is a blue "squiggly" line under the "lbx_Names" portion of this code:
               lbx_Names.Items(0).text = "Betty"
...and this occurs at every reference to the "lbx_Names" object.  

The mouse-over message states "lbx_Names is NOT DECLARED"

2.) I get the "Index was out of range" error message  because (I think?) it does not even SEE the asp:ListBox object and is (instead) using the (2) items in the asp:RadioButtonList. Hence when it reaches the third item i get the error.

I just dont understand WHY i am getting this error. Is there an error in the book? Maybe. But not likely. So what am I missing.

=========
The .aspx file
=========

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="day5_ex_01.aspx.vb" Inherits="WebApplication1.day5_ex_01"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>day5_ex_01</title>
        <meta name="vs_snapToGrid" content="False">
        <meta name="vs_showGrid" content="False">
        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="pagelayout">
        <form id="Form1" method="post" runat="server"> <!-- begin form -->
         <!-- create the instructions -->
         <h1 style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 'Trebuchet MS', Verdana, Sans-Serif">
             Choose your baby's name</h1>
         <!-- write exercise instructions -->
         <p>Build an application for choosing a baby's name. The user
         will select sex of the baby, and THEN a listbox will display
         a few possible names. When the user selects a name, a message
         will be displayed using the selected name.</p>
         
         <!-- build radio button list -->
         <p>Choose your Baby's Sex
         <asp:RadioButtonList ID="rbl_chooseSex"  Runat="server"
         AutoPostBack=True OnSelectedIndexChanged="changeName"
         RepeatDirection="Horizontal">
          <asp:ListItem>Female</asp:ListItem>
          <asp:ListItem>Male</asp:ListItem>
         </asp:RadioButtonList></p>
         
         
         <asp:ListBox ID="lbx_Names" Runat="server" AutoPostBack="True" >
          <asp:ListItem></asp:ListItem>
          <asp:ListItem></asp:ListItem>
          <asp:ListItem></asp:ListItem>
          <asp:ListItem></asp:ListItem>
         </asp:ListBox>
        </form>
    </body>
</HTML>

=======
The .vb file
=======
    ' create a subroutine to change the names based on the selected gender
    Sub changeName(ByVal sender As Object, ByVal e As EventArgs)

        'Step 01 - make sure there are no entries in the index
        If rbl_chooseSex.SelectedIndex = 0 Then
            'female
            lbx_Names.Items(0).text = "Betty"
            lbx_Names.Items(1).text = "Barbara"
            lbx_Names.Items(2).text = "Betsy"
            lbx_Names.Items(3).text = "Brenda"

        Else
            ' male
            lbx_Names.Items(0).text = "John"
            lbx_Names.Items(1).text = "Joseph"
            lbx_Names.Items(2).text = "James"
            lbx_Names.Items(3).text = "Jebediah"
        End If
    End Sub

===========
ERROR MESSAGE
===========
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Source Error:

Line 36:             lbx_Names.Items(1).text = "Barbara"
Line 37:             lbx_Names.Items(2).text = "Betsy"
Line 38:             lbx_Names.Items(3).text = "Brenda"
Line 39:
Line 40:         Else
======================

thanks in advance!
the_sleeperAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
tovvenkiConnect With a Mentor Commented:
Hi,
the problem is each time when you update the design you need to close the code behind file, save the design and reopen the code view by double clicking on the design window. When you do this vs.net will update the code view will all the changes that you have made in the design. so that you won't get the message "lbx_Names is NOT DECLARED"

Regards,
venki
0
 
the_sleeperAuthor Commented:
ok. this IS weird.  NOW its working. I changed...nothing. is there a compile issue? some other VS.net issue?
0
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

  I copy paste ur code, and its' working fine, maybe  u were missing a thing or mmm i really don't know
but your code working well without any problem

enjoy learning ;-)
0
 
mmarinovCommented:
Hi,

if it is working now, may be the problem was that you did not rebuild the project and the web server don't find all of the items and gives you this error

Regards,
B..M
0
 
the_sleeperAuthor Commented:
thanks gang! More questions coming!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.