Re: DropDownLists and NULL values




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" <neil.jellis@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1138604752.204475.309960@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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
>


.



Relevant Pages

  • DropDownLists and NULL values
    ... 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. ... obviously not got the 0 value insertion code in the right place. ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Failed to load viewstate
    ... I used a SqlDataSource control to bind the three dropdownlists on ... protected void SearchButton_Click ... you can also post the test custom class(which used to bind ... This posting is provided "AS IS" with no warranties, and confers no rights. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • FormView and DropDownList
    ... I have a problem with formviews and DropDownLists in ASP.NET 2.0. ... database that simply selects all drivers from a database table. ... easier for me to enter several routes for one driver. ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • FormView and DropDownList
    ... I have a problem with formviews and DropDownLists in ASP.NET 2.0. ... database that simply selects all drivers from a database table. ... easier for me to enter several routes for one driver. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: multiple dropdown lists = multiple data readers???
    ... Read the data once, into an array or arraylist, and bind all the ... dropdownlists to that. ... > with the prospect of having to run the above routine 10 times each for ...
    (microsoft.public.dotnet.framework.aspnet)