Solved

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

Posted on 2004-08-18
5
1,059 Views
Last Modified: 2012-08-13
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!
0
Comment
Question by:the_sleeper
5 Comments
 

Author Comment

by:the_sleeper
ID: 11837964
ok. this IS weird.  NOW its working. I changed...nothing. is there a compile issue? some other VS.net issue?
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 11838541
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 11838648
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
 
LVL 21

Accepted Solution

by:
tovvenki earned 500 total points
ID: 11839529
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
 

Author Comment

by:the_sleeper
ID: 11844928
thanks gang! More questions coming!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question