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. Advertisements

  2. Neil

    Rote Rote Guest

    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
    Rote Rote, Jan 30, 2006
    1. Advertisements

  3. I'd like to know what the best method is for handling NULL (or 0)
    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 );


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

    Neil Guest

    What you can do is don't let your selected value go to the DB unless
    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
  5. Neil

    Guest Guest

    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.

    Guest, Jan 30, 2006
  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:

    (They don't do the explicit ListItem.Value of 0 like I did, but its the same
    sloan, Jan 31, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.