Professional Web Applications Themes

Listbox & Object Not Referenced - ASP.NET General

All When I try to get the text from my listbox I am get an error which is listed below. Any ideas? Thanks Object reference not set to an instance of an object. 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.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 83: 'Need to bind the textboxes from the choice picked out from the list Line 84: ' Dim ListItem As ...

  1. #1

    Default Listbox & Object Not Referenced

    All

    When I try to get the text from my listbox I am get an
    error which is listed below.

    Any ideas?

    Thanks

    Object reference not set to an instance of an object.
    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.NullReferenceException: Object
    reference not set to an instance of an object.

    Source Error:


    Line 83: 'Need to bind the textboxes from the
    choice picked out from the list
    Line 84: ' Dim ListItem As ListItem =
    lstUsers.SelectedItem
    Line 85: UserName.Text = stUsers.SelectedItem.Text
    Line 86:
    Line 87: 'Get the user details and fill textboxes

    Source File: C:\Dev\Asset Register
    SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
    Register\AssetRegister\ViewUsers.aspx.vb Line: 85

    Stack Trace:


    [NullReferenceException: Object reference not set to an
    instance of an object.]
    AssetRegister.ViewUser.lstUsers_SelectedIndexChang ed
    (Object sender, EventArgs e) in C:\Dev\Asset Register
    SW\SourceCode\DotNet\ASP.NET\ASP.NET Asset
    Register\AssetRegister\ViewUsers.aspx.vb:85

    System.Web.UI.WebControls.ListControl.OnSelectedIn dexChang
    ed(EventArgs e)

    System.Web.UI.WebControls.ListBox.System.Web.UI.IP ostBackD
    ataHandler.RaisePostDataChangedEvent()
    System.Web.UI.Page.RaiseChangedEvents()
    System.Web.UI.Page.ProcessRequestMain()





    yop Guest

  2. #2

    Default Re: Listbox & Object Not Referenced

    Unless an Item has been selected, this value will be null.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    The more I learn, the less I know.

    "yop" <net> wrote in message
    news:0a6401c3688d$d600cd00$gbl... 


    Kevin Guest

  3. #3

    Default Re: Listbox & Object Not Referenced

    Kevin

    I have an item selected alright, daft idea but could it
    be something to do with postback or something?

    I click on the item and call the following:

    Private Sub lstUsers_SelectedIndexChanged(ByVal sender As
    Object, ByVal e As System.EventArgs) _
    Handles lstUsers.SelectedIndexChanged

    'Need to bind the textboxes from the choice picked out
    from the list
    UserName.Text = lstUsers.SelectedItem.Text


    thanks
     
    null. [/ref]
    Object [/ref]
    stUsers.SelectedItem.Text [/ref]
    textboxes [/ref]
    System.Web.UI.WebControls.ListControl.OnSelectedIn dexChang [/ref]
    System.Web.UI.WebControls.ListBox.System.Web.UI.IP ostBackD 
    >
    >
    >.
    >[/ref]
    yop Guest

  4. #4

    Default Re: Listbox & Object Not Referenced

    Try using .SelectedValue property.....

    TextBox1.Text = ListBox1.SelectedValue()

    If it fails too, i would check whether it is selected the
    first time user comes on the web page. If that is also
    set, and it is not working, i will try to see
    whether "value" property returns anything. If that fails,
    i have no idea why its not working.....

    Since it is going to "SelectedIndexChanged" event i am
    hoping that the "AutoPostBack" is set to true for your
    control.

    Thanks,

    Tapasvi




     
    >null. [/ref]
    >Object [/ref]
    >stUsers.SelectedItem.Text [/ref]
    >textboxes [/ref]
    >System.Web.UI.WebControls.ListControl.OnSelectedI ndexChang [/ref]
    >System.Web.UI.WebControls.ListBox.System.Web.UI.I PostBackD 
    >>
    >>
    >>.
    >>[/ref]
    >.
    >[/ref]
    Tapasvi Guest

  5. #5

    Default Re: Listbox & Object Not Referenced

    Is the ListBox's AutoPostBack property set to True?

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    The more I learn, the less I know.

    "yop" <net> wrote in message
    news:04a101c368aa$b7ce3760$gbl... 
    > null. [/ref]
    > Object [/ref]
    > stUsers.SelectedItem.Text [/ref]
    > textboxes [/ref]
    > System.Web.UI.WebControls.ListControl.OnSelectedIn dexChang [/ref]
    > System.Web.UI.WebControls.ListBox.System.Web.UI.IP ostBackD 
    > >
    > >
    > >.
    > >[/ref][/ref]


    Kevin Guest

  6. #6

    Default Re: Listbox & Object Not Referenced

    Another possiblity: Is the ListBox dynamically added to the Page?

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    The more I learn, the less I know.

    "yop" <net> wrote in message
    news:04a101c368aa$b7ce3760$gbl... 
    > null. [/ref]
    > Object [/ref]
    > stUsers.SelectedItem.Text [/ref]
    > textboxes [/ref]
    > System.Web.UI.WebControls.ListControl.OnSelectedIn dexChang [/ref]
    > System.Web.UI.WebControls.ListBox.System.Web.UI.IP ostBackD 
    > >
    > >
    > >.
    > >[/ref][/ref]


    Kevin Guest

  7. #7

    Default Re: Listbox & Object Not Referenced

    Yes it is a property. Here is the code that works fine on
    my machine.....

    Code behind >>>>>

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
    System.Object, ByVal e As System.EventArgs)
    Handles ListBox1.SelectedIndexChanged
    TextBox1.Text = ListBox1.SelectedValue()
    End Sub

    ASPX code >>>>>

    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <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="GridLayout">
    <form id="Form1" method="post"
    runat="server">
    <asp:ListBox id="ListBox1"
    style="Z-INDEX: 101; LEFT: 288px; POSITION: absolute; TOP:
    168px"
    runat="server"
    Width="160px" Height="168px" AutoPostBack="True">
    <asp:ListItem
    Value="Tapasvi">Tapasvi</asp:ListItem>
    <asp:ListItem Value="Tap
    at Work">Tap at Work</asp:ListItem>
    </asp:ListBox>
    <asp:TextBox id="TextBox1"
    style="Z-INDEX: 102; LEFT: 288px; POSITION: absolute; TOP:
    120px"

    runat="server"></asp:TextBox>
    </form>
    </body>
    </HTML>

    Thanks,

    Tap



     [/ref]
    >As [/ref]
    >lstUsers.SelectedIndexChanged [/ref]
    >an [/ref]
    >object. [/ref]
    >the [/ref]
    >the [/ref]
    >the [/ref]
    >to an [/ref]
    >AssetRegister.ViewUser.lstUsers_SelectedIndexChan ged [/ref]
    >Register [/ref]
    >System.Web.UI.WebControls.ListControl.OnSelectedI ndexChang [/ref]
    >System.Web.UI.WebControls.ListBox.System.Web.UI.I PostBackD 
    >>
    >>
    >>.
    >>[/ref]
    >.
    >[/ref]
    Tapasvi Guest

  8. #8

    Default Re: Listbox & Object Not Referenced

    No the listbox is added at design time but the items are
    added from the DB at run time. Maybe that is an issue.
    Extremely frustating!! :(

    Also the selectedvalue property is telling it is "not a
    member of System.Web.UI.WebControls.Listbox.

    I am using Framework 1.1, there is an issue with using
    the webcontrols and this version I think??

    Would I be better of using a 3rd party list or data list
    or something?



     
    on 
    As 
    content="Visual 
    TOP: 
    TOP: [/ref][/ref]
    it [/ref][/ref]
    sender 
    >>lstUsers.SelectedIndexChanged [/ref][/ref]
    out [/ref][/ref]
    get 
    >>object. [/ref][/ref]
    during [/ref][/ref]
    review [/ref][/ref]
    and 
    >>the [/ref][/ref]
    85 
    >>to an 
    >>AssetRegister.ViewUser.lstUsers_SelectedIndexCha nged 
    >>Register 
    >>System.Web.UI.WebControls.ListControl.OnSelected IndexCha[/ref][/ref]
    ng 
    >>System.Web.UI.WebControls.ListBox.System.Web.UI. IPostBac[/ref][/ref]
    kD 
    >>.
    >>[/ref]
    >.
    >[/ref]
    yop Guest

  9. #9

    Default Listbox & Object Not Referenced

    eventually found the fix after trawling the forums!!

    "I was loading the listbox dynamically on the page_load
    event.
    Eventually I discovered - that was too late,
    The items showed on the page but weren't really
    there..... (figure that one
    out)
    When I moved the list populate routine to the page_init
    event everything
    worked great."

    Thanks for all your help


     
    stUsers.SelectedItem.Text 


    yop Guest

Similar Threads

  1. click listbox and refresh another listbox
    By DC Gringo in forum ASP.NET Building Controls
    Replies: 0
    Last Post: April 6th, 02:13 AM
  2. Item label displays as "," when moving from listbox to listbox
    By quadrant6 webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: January 27th, 02:45 AM
  3. Binding custom object to a listbox
    By Fraggle in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: November 4th, 02:32 PM
  4. Replies: 0
    Last Post: August 3rd, 10:18 PM
  5. Help with ListBox Object . . . Please
    By Twig in forum Microsoft Access
    Replies: 3
    Last Post: July 10th, 11:27 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139