DropDownLists and NULL values

Discussion in 'ASP .Net' started by Neil, Jan 30, 2006.

  1. Neil

    Neil Guest

    I'd like to know what the best method is for handling NULL (or 0)
    values within a DropDownList.

    If a database lookup value is optional I would normally consider
    leaving it NULL but this leads to problems with DropDownLists because
    there is no value to bind to and an eror is raised.

    I considered a method whereby the DropDownList has a an index value of
    0 inserted with a space character as the text after it has retrieved
    the data from the source table. As it has a 0 index value it would
    appear at the top of the list and would indicate that no selection had
    been made. Within the database the column would be defined with a
    default value of 0. Thus the 0 value retrieved from the db would bind
    to the 0 value inserted into the DropDownList.

    I've tried to get this approach to work but the code always errors
    because there is no value to bind the retrieved db value against. I've
    obviously not got the 0 value insertion code in the right place.

    The web form is filling a formview control via a sqldatasource
    following the selection of a record on a gridview.


    Can someone point me in the right direction as to how such optional
    lookup data in a dropdownlist should be handled
     
    Neil, Jan 30, 2006
    #1
    1. Advertising

  2. Neil

    Rote Rote Guest

    Neil,
    Try posting some snippet code i'm sure it would help people here to
    help you.
    What you can do is don't let your selected value go to the DB unless
    some option value has been selected.
    use ddl.selectedindex(this gives you the index of the DropDownList)
    Hope that helps
    Patrick

    *** Sent via Developersdex http://www.developersdex.com ***
     
    Rote Rote, Jan 30, 2006
    #2
    1. Advertising

  3. > I'd like to know what the best method is for handling NULL (or 0)
    > values within a DropDownList.


    I also had this problem when binding to an object layer (ObjectDataSource).
    my solution is as follows:

    1) in the OnDataBound event of the dropdown I call a method that inserts an
    element to the top of the list (["empty", "empty"])

    2) I set the SelectedValue:

    SelectedValue='<%# Eval("expression")!=null ? Eval("expression").ToString()
    : null %>'>

    note that when null is used as the SelectedValue, the top most element will
    be choosen (in this case it is the ["empty", "empty"] element.

    3) I read the SelectedValue:

    string v_exp = DropDown.SelectedValue;
    int? id = v_exp == "empty" ? new Nullable<int>() : int.Parse( v_exp );



    Regards,

    Wiktor Zychla
     
    Wiktor Zychla [C# MVP], Jan 30, 2006
    #3
  4. Neil

    Neil Guest

    >What you can do is don't let your selected value go to the DB unless
    >some option value has been selected.


    Is it possible to do this when using a sqldatasource with a formview?
    Wouldn't this approach mean that you'd have to change the update
    statement assigned to the sqldatasource to remove the value of any
    columns fed by a dropdownlist that hadn't been selected?

    Rather than looking for a specific code resolution I was interested
    more in the approach I should be taking based upon other's practices.
    After all, this must be a common issue that developers handle.
     
    Neil, Jan 30, 2006
    #4
  5. The best way is to test this way If(!object.Equals("obj1", null));
    This gives the perfect ans. Any doubts then pls contact MICROSOFT!
    --
    Shrinivas Reddy.
    Systems Analyst
    Satyam Computer Services Ltd.


    "Wiktor Zychla [C# MVP]" wrote:

    > > I'd like to know what the best method is for handling NULL (or 0)
    > > values within a DropDownList.

    >
    > I also had this problem when binding to an object layer (ObjectDataSource).
    > my solution is as follows:
    >
    > 1) in the OnDataBound event of the dropdown I call a method that inserts an
    > element to the top of the list (["empty", "empty"])
    >
    > 2) I set the SelectedValue:
    >
    > SelectedValue='<%# Eval("expression")!=null ? Eval("expression").ToString()
    > : null %>'>
    >
    > note that when null is used as the SelectedValue, the top most element will
    > be choosen (in this case it is the ["empty", "empty"] element.
    >
    > 3) I read the SelectedValue:
    >
    > string v_exp = DropDown.SelectedValue;
    > int? id = v_exp == "empty" ? new Nullable<int>() : int.Parse( v_exp );
    >
    >
    >
    > Regards,
    >
    > Wiktor Zychla
    >
    >
     
    =?Utf-8?B?U2ludQ==?=, Jan 30, 2006
    #5
  6. Neil

    sloan Guest

    I usually do a
    ddlMyDDL.Items.insert(0 , new ListItem(0 , "--Select--"))

    I'm going from memory, but what I mean is a new ListItem with the value of
    0, text of "--Select--", and I use the Items.Insert (at position 0)
    (which I do immediately AFTER the DataBind() method)

    That may not be what you mean...as in..if the value coming from the DB is
    null to begin with or something.

    ...

    But what I propose will put that "fake" value in there.

    If my database doesn't actually like a zero, in my businesslayer, I will
    convert a 0 to DBNULL ...

    ...
    Ok, I just did a quick google, and found this:
    http://www.4guysfromrolla.com/webtech/073101-1.shtml

    (They don't do the explicit ListItem.Value of 0 like I did, but its the same
    concept)




    "Neil" <> wrote in message
    news:...
    > I'd like to know what the best method is for handling NULL (or 0)
    > values within a DropDownList.
    >
    > If a database lookup value is optional I would normally consider
    > leaving it NULL but this leads to problems with DropDownLists because
    > there is no value to bind to and an eror is raised.
    >
    > I considered a method whereby the DropDownList has a an index value of
    > 0 inserted with a space character as the text after it has retrieved
    > the data from the source table. As it has a 0 index value it would
    > appear at the top of the list and would indicate that no selection had
    > been made. Within the database the column would be defined with a
    > default value of 0. Thus the 0 value retrieved from the db would bind
    > to the 0 value inserted into the DropDownList.
    >
    > I've tried to get this approach to work but the code always errors
    > because there is no value to bind the retrieved db value against. I've
    > obviously not got the 0 value insertion code in the right place.
    >
    > The web form is filling a formview control via a sqldatasource
    > following the selection of a record on a gridview.
    >
    >
    > Can someone point me in the right direction as to how such optional
    > lookup data in a dropdownlist should be handled
    >
     
    sloan, Jan 31, 2006
    #6
    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. Replies:
    5
    Views:
    26,709
    Mike Schilling
    Mar 29, 2006
  2. Nathan Sokalski
    Replies:
    0
    Views:
    301
    Nathan Sokalski
    Aug 23, 2008
  3. Nathan Sokalski
    Replies:
    0
    Views:
    410
    Nathan Sokalski
    Aug 26, 2008
  4. Nathan Sokalski
    Replies:
    0
    Views:
    854
    Nathan Sokalski
    Aug 26, 2008
  5. Nathan Sokalski
    Replies:
    0
    Views:
    187
    Nathan Sokalski
    Aug 26, 2008
Loading...

Share This Page