473,585 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropDownLists and NULL values

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

Jan 30 '06 #1
5 6402
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.selectedind ex(this gives you the index of the DropDownList)
Hope that helps
Patrick

*** Sent via Developersdex http://www.developersdex.com ***
Jan 30 '06 #2
> 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 (ObjectDataSour ce).
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("expressio n")!=null ? Eval("expressio n").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.Select edValue;
int? id = v_exp == "empty" ? new Nullable<int>() : int.Parse( v_exp );

Regards,

Wiktor Zychla

Jan 30 '06 #3
>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.

Jan 30 '06 #4
The best way is to test this way If(!object.Equa ls("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 (ObjectDataSour ce).
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("expressio n")!=null ? Eval("expressio n").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.Select edValue;
int? id = v_exp == "empty" ? new Nullable<int>() : int.Parse( v_exp );

Regards,

Wiktor Zychla

Jan 30 '06 #5

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" <ne*********@sy napse-partnership.com > wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
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

Jan 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3034
by: Marcus | last post by:
Hello, quick question about MySQL storing NULL values... Say I have a textbox called $_POST and a variable $var. if(empty($_POST)) $var = NULL; else $var = $_POST; Disregarding filtering/formatting the data, upon inserting $var into
26
45391
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a...
12
1773
by: D Witherspoon | last post by:
What is the accepted method of creating a data class or business rules object class with properties that will allow the returning of null values? For example... I have a class named CResults with the following properties. TestID int QuestionID int AnswerID int So, this is a simple example, but I want to be able to know if AnswerID is
8
12531
by: manning_news | last post by:
Using SQL2000. According to Books Online, the avg aggregrate function ignores null values. ((3+3+3+3+Null)/5) predictably returns Null. Is there a function to ignore the Null entry, adjust the divisor, and return a value of 3? For example:((3+3+3+3)/4) after ignoring Null entry. If there's more than one null value, then adjust divisor...
3
4007
by: JOEP | last post by:
What do I need to do to allow an append query to post null values to records in a field of the destination table? Basically I want to allow records with null values to post to the table. The append query will not work unless there are values in the data i am attempting to send. I want the fields in the destination table to accept null and...
3
5803
by: Rico | last post by:
Hello, I have a foreign key constraint between two tables (Appointments and MissedAppointmentReasons) and I'd like to allow null values in Appointments table for the field containing the MissedAppointmentReason, but currently, I get a Foreign Key Constraint Error when I try to add a record to the Appointments table. Any ideas how I can...
0
1108
by: Nightcrawler | last post by:
I have a table adapter that looks like this: TableAdapter.GetData(string name, string email, ing countryid, int stateId) This is calling the following stored procedure SELECT * FROM Table WHERE Name= COALESCE(@Name,Name)
0
1762
by: gp | last post by:
I am and have been using PDO for about a year now...and have finally gotten around to solving the "DB NULL value" issues I ran into early on... I am looking for suggestions and techniques to deal with inserting DB NULL values into my MySQL 5.x DB Tables....I am running PHP 5.2.x on BSD 6.x with Apache 2.2.x.... As mentioned I am writing...
10
9014
by: Toby Gallier | last post by:
Hello! I have a form that is calculating averages as follows: " =(NZ()+Nz()+Nz())/3 " However I need to now adjust for null values , so for example if value2 is null I would then need to base my average on just 2 values instead of 3 i am currently using in my string. How can i have the form update the "3" based on the number of values...
10
2455
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have the following three files. 1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to populate a simple <asp:ListBoxcontrol. 2. The UserDetails.cs file creates a Namespace named UserComponents and creates an object named UserDetails. 3. The UserDB.cs file retrieves the actual data from the database. The code below has...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.