DropDownLists and NULL values | Bytes (2024)

Neil

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

Subscribe Reply

5 DropDownLists and NULL values | Bytes (1) 6373 DropDownLists and NULL values | Bytes (2)

Rote Rote

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

Wiktor Zychla [C# MVP]

> 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

Neil

>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

Sinu

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

sloan

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 3027

mysql and null values

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

PHP

26 45383

Unique constraint and NULL values

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

Oracle Database

12 1763

Common practice, allowing null values in data classes.

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

.NET Framework

8 12523

Getting Avg to really ignore null values

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

Microsoft SQL Server

3 4005

Append Import Accept Null Values

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

Microsoft Access / VBA

3 5801

SS 2005 Express- Allowing null values and Foriegn Key Constraints

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

Microsoft SQL Server

1105

DropDownLists and NULL values

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)

C# / C Sharp

1760

techniques for Inserting DB NULL values (PDO is what i use now for "all" DML)

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

PHP

10 9009

average with null values

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

Microsoft Access / VBA

10 2447

Null Values stopping code module.

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

C# / C Sharp

7478

What is ONU?

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

General

7410

Changing the language in Windows 10

by: Hystou |last post by:

Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...

Windows Server

7923

Maximizing Business Potential: The Nexus of Website Design and Digital Marketing

by: jinu1996 |last post by:

In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...

Online Marketing

1 7437

The easy way to turn off automatic updates for Windows 10/11

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

Windows Server

7773

Discussion: How does Zigbee compare with other wireless protocols in smart home applications?

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

General

4960

Couldn’t get equations in html when convert word .docx file to html file in C#.

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

C# / C Sharp

3466

Trying to create a lan-to-lan vpn between two differents networks

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

Networking - Hardware / Configuration

1 1025

How to add payments to a PHP MySQL app.

by: muto222 |last post by:

How can i add a mobile payment intergratation into php mysql website.

PHP

722

Comprehensive Guide to Website Development in Toronto: Expert Insights from BSMN Consultancy

by: bsmnconsultancy |last post by:

In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

General

DropDownLists and NULL values | Bytes (2024)

FAQs

What is a null value answer? ›

A null value in a relational database is used when the value in a column is unknown or missing. A null is neither an empty string (for character or datetime data types) nor a zero value (for numeric data types).

Do empty strings take up RAM? ›

Even a string of length zero can require memory to store it, depending on the format being used. In most programming languages, the empty string is distinct from a null reference (or null pointer) because a null reference points to no string at all, not even the empty string.

What is an example of a NULL value? ›

A null indicates a lack of a value, which is not the same thing as a zero value. For example, consider the question "How many books does Adam own?" The answer may be "zero" (we know that he owns none) or "null" (we do not know how many he owns).

What is a NULL answer? ›

"Null" essentially means that the question has not been answered. " 0" is an. answer, you don't want to confuse the two.

What is the difference between null and empty string? ›

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

Is it better to use string empty? ›

Use String. Empty when emphasizing clarity and readability in your code.In scenarios where multiple instances of an empty string might be used, String. Empty can lead to better performance due to its one-time initialization.

Why do we use empty strings? ›

An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.

What is considered a NULL value? ›

A NULL value is used to represent a missing value, but it usually has one of three different interpretations: The value unknown (value exists but is not known) Value not available (exists but is purposely withheld) Attribute not applicable (undefined for this tuple)

What is a NULL response? ›

A null response indicates that there was no successful connection when you attempt to connect to our gateway."

Is NULL value 1 or 0? ›

Unlike zero, null values are not an absolute or something quantifiable; they're an unknown. As such they should not be stored as numbers (i.e. 0, -999, 999, etc.).

Is NULL 0 or 1? ›

The answer to that is rather simple: a NULL means that there is no value, we're looking at a blank/empty cell, and 0 means the value itself is 0.

Top Articles
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 5355

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.