[thelist] Null Value from a database to a string?

Ken Schaefer Ken at adOpenStatic.com
Thu Dec 30 06:46:22 CST 2004


I would add a caveat that whilst it may be easier to "disallow" NULL values,
a NULL usually has a special significance within a database (signifying an
"unknown" or "undefined" value). And so, if your schema or requirements call
for allowing these values then you need to keep using NULL values. Doing
what's "easiest" isn't always the "best" way of doing something :-) Too many
times you see people using arbitrary values (eg 1/1/1900) as the "unknown"
value, but it comes back to bite them later on.

Cheers
Ken

: -----Original Message-----
: From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On
: Behalf Of David Travis
: Sent: Thursday, 30 December 2004 6:37 PM
: To: thelist at lists.evolt.org
: Subject: RE: [thelist] Null Value from a database to a string?
: 
: Casey,
: 
: In addition to the solution I offered I must add that setting a default
: value in this column so an empty field will be an empty string and not null
: provides much easier code.
: 
: Note that some databases require special handling when it comes to null
: values, so either way it is recommended (sometimes) not to allow them by
: using default values.
: 
: David Travis.
: 
: -----Original Message-----
: From: thelist-bounces at lists.evolt.org
: [mailto:thelist-bounces at lists.evolt.org] On Behalf Of David Travis
: Sent: Wednesday, December 29, 2004 10:28 PM
: To: 'Casey Crookston'; thelist at lists.evolt.org
: Subject: RE: [thelist] Null Value from a database to a string?
: 
: Hi Casey,
: 
: You should have something like this:
: 
: Dim adrs2 as String
: If TypeOf myRow["adrs2"] Is DbNull Then
: 	adrs2 = String.Empty
: Else
: 	adrs2 = myRow["adrs2"].ToString()
: End If
: 
: I am writing this code without testing it, and it was a while since I
worked
: with VB.NET, so I am not sure this is 100% working, but that's the main
: idea.
: 
: Good Luck!
: David Travis.
: 
: 
: -----Original Message-----
: From: thelist-bounces at lists.evolt.org
: [mailto:thelist-bounces at lists.evolt.org] On Behalf Of Casey Crookston
: Sent: Wednesday, December 29, 2004 7:36 PM
: To: thelist at lists.evolt.org
: Subject: [thelist] Null Value from a database to a string?
: 
: asp.net in vb...
: 
: I'm populating a form from a database row.  There's a column in the db for
: address and address2 incase the user needs a second line for their address.
: In the db address2 is allowed a null value, but when trying to populate the
: form I get this error:
: 
: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Cast from type 'DBNull' to type 'String' is not valid.
: Description: An unhandled exception occurred during the execution of the
: current web request. Please review the stack trace for more information
: about the error and where it originated in the code.
: 
: Exception Details: System.InvalidCastException: Cast from type 'DBNull' to
: type 'String' is not valid.
: 
: Source Error:
: 
: Line 95:   baddress2.text=mySingleMemberDtr("b_address2")
: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: 
: Makes sense, I guess.  It won't let a null value be passed into a text
: string.  What can I do to allow a null value to be entered. I REALLY don't
: want to have to force the user to enter something like "n/a" into the field
: if it is empty.  Or, what's the best way to have a value look empty when
: it's not?
: 
: Thanks!
: 
: 
: --
: 
: * * Please support the community that supports you.  * *
: http://evolt.org/help_support_evolt/
: 
: For unsubscribe and other options, including the Tip Harvester
: and archives of thelist go to: http://lists.evolt.org
: Workers of the Web, evolt !
: 
: --
: 
: * * Please support the community that supports you.  * *
: http://evolt.org/help_support_evolt/
: 
: For unsubscribe and other options, including the Tip Harvester
: and archives of thelist go to: http://lists.evolt.org
: Workers of the Web, evolt !
: 
: --
: 
: * * Please support the community that supports you.  * *
: http://evolt.org/help_support_evolt/
: 
: For unsubscribe and other options, including the Tip Harvester
: and archives of thelist go to: http://lists.evolt.org
: Workers of the Web, evolt !


More information about the thelist mailing list