System.NullReferenceException:

Discussion in 'ASP .Net' started by Brian, Mar 30, 2005.

  1. Brian

    Brian Guest

    i have a radiobuttonlist. I want to check to see if a button has been
    selected then get the value.. This was my code;

    ----------------------------------------------------------------

    Dim NewGender as String
    if (Gender.SelectedItem.Value <> "") then
    NewGender = Gender.SelectedItem.Value

    else

    NewGender = ""

    end if

    If nothing is selected, I get the following error..

    System.NullReferenceException: Object reference not set to an instance
    of an object.

    thank in advance..
    Brian, Mar 30, 2005
    #1
    1. Advertising

  2. Brian

    Marina Guest

    Yes, if no item is selected then you get the error. Wouldn't you expect to
    get it if you are checking the Value property of SelectedItem, but
    SelectedItem is null?
    What is your question exactly?

    "Brian" <> wrote in message
    news:...
    >i have a radiobuttonlist. I want to check to see if a button has been
    > selected then get the value.. This was my code;
    >
    > ----------------------------------------------------------------
    >
    > Dim NewGender as String
    > if (Gender.SelectedItem.Value <> "") then
    > NewGender = Gender.SelectedItem.Value
    >
    > else
    >
    > NewGender = ""
    >
    > end if
    >
    > If nothing is selected, I get the following error..
    >
    > System.NullReferenceException: Object reference not set to an instance
    > of an object.
    >
    > thank in advance..
    >
    Marina, Mar 30, 2005
    #2
    1. Advertising

  3. I needed to check if there was a value or not, so I wouldn't get an
    error when I posted the value..

    I figured it out though.. i needed to to Gender.SelectedValue.. I am
    kinda new to this, so I am still learning.. Thanks for your response..

    *** Sent via Developersdex http://www.developersdex.com ***
    Brian Ciarcia, Mar 30, 2005
    #3
  4. Brian

    Elton Wang Guest

    Hi Brian,

    1. You can set a default value for radiobuttonlist.

    2. Or you can

    If Not Gender.SelectedItem Is Nothing Then
    if (Gender.SelectedItem.Value <> "") then
    ' ...

    End If



    HTH

    Elton Wang




    >-----Original Message-----
    >i have a radiobuttonlist. I want to check to see if a

    button has been
    >selected then get the value.. This was my code;
    >
    >----------------------------------------------------------

    ------
    >
    >Dim NewGender as String
    >if (Gender.SelectedItem.Value <> "") then
    >NewGender = Gender.SelectedItem.Value
    >
    >else
    >
    >NewGender = ""
    >
    >end if
    >
    >If nothing is selected, I get the following error..
    >
    >System.NullReferenceException: Object reference not set

    to an instance
    >of an object.
    >
    >thank in advance..
    >
    >.
    >
    Elton Wang, Mar 30, 2005
    #4
  5. Brian,

    I've been doing some research on radiobuttonlists and from what i've read
    and the examples i've looked at, i have not yet found a way to handle an
    "unselected" radiobuttonlist.

    Are you trying to update a database or just trying to display what the user
    has selected?

    Also, i'd be interested to know if you have found a way to update a
    database (even if it's a null value) if no items in a radiobuttonlist have
    been selected.

    Thanks.

    --
    Message posted via http://www.dotnetmonster.com
    Sim0n Templar via DotNetMonster.com, Mar 30, 2005
    #5
    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. William
    Replies:
    0
    Views:
    1,586
    William
    Aug 10, 2004
  2. msnews.microsoft.com

    Help with System.NullReferenceException

    msnews.microsoft.com, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    564
    msnews.microsoft.com
    Jul 16, 2003
  3. Manfred Braun
    Replies:
    3
    Views:
    750
    Manfred Braun
    Aug 30, 2003
  4. Angel Faus
    Replies:
    2
    Views:
    980
    Jeff Washburn
    Dec 1, 2003
  5. PatricQ
    Replies:
    3
    Views:
    9,544
    PatricQ
    Feb 18, 2004
Loading...

Share This Page