Solved

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

Posted on 2004-08-18
5
1,064 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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