How do you handle nullable strings

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

Can you handle nullable string vb.net 2.0?

You can with Integer but I get an error when I try to do a

Dim s as Nullable (Of String)

Now strings are a nullable type but it can't handle:

fn.FormName = dbReader("FormName")

You get an error saying:

Unable to cast object of type 'System.DBNull' to type 'System.String'.

So do I still need to test for null and set to "" or null if this is the
case?

Thanks,

Tom
 
Strings are classes, and therefore you don't need to use the Nullable keword
with them. You've always had the ability to have null srings in .NET.

To test if a string is null or not, you've always been able to test for
"null" (C#) or "Nothing" (VB .NET). Now, in .NET 2.0, you can use the
String object's IsNullOrEmpty property:

Dim s As String
If String.IsNullOrEmpty(s) Then....

-Scott
 
tshad said:
Can you handle nullable string vb.net 2.0?

You can with Integer but I get an error when I try to do a

Dim s as Nullable (Of String)

Now strings are a nullable type but it can't handle:

fn.FormName = dbReader("FormName")

You get an error saying:

Unable to cast object of type 'System.DBNull' to type 'System.String'.

\\\
If dbReader(...) Is DBNull.Value Then
...
Else
... = DirectCast(dbReader(...), String)
End If
///
 
Can you handle nullable string vb.net 2.0?

You can with Integer but I get an error when I try to do a

Dim s as Nullable (Of String)

Now strings are a nullable type but it can't handle:

fn.FormName = dbReader("FormName")

You get an error saying:

Unable to cast object of type 'System.DBNull' to type 'System.String'.

So do I still need to test for null and set to "" or null if this is the
case?

Thanks,

Tom

The null you get back from a database is DBNull, which is entirely
different from the language null (null or Nothing).

You must explicitly test for DBNull before trying to assign.
 
Tshad,

A non initialized value in memory has not much to do with an empty field in
a database.

DBNull tells that there is actual nothing in the DataBase field,

Nothing tells that there is no refererence (and therefore as well with a
strings)

Null tells that a value field is not even been initialized to its default
(which is the standard behaviour of VB).

Nothing when initialized at values (including strings) tells that it is the
default value.

Cor
 
tshad said:
Can you handle nullable string vb.net 2.0?

You can with Integer but I get an error when I try to do a

Dim s as Nullable (Of String)

Now strings are a nullable type but it can't handle:

fn.FormName = dbReader("FormName")

You get an error saying:

Unable to cast object of type 'System.DBNull' to type 'System.String'.

So do I still need to test for null and set to "" or null if this is
the case?

Side-stepping the problem, in the SQL query you could use
coalesce(dbFieldName, '') to get the DB to convert nulls to empty strings.

Andrew
 
Can you handle nullable string vb.net 2.0?

You can with Integer but I get an error when I try to do a

Dim s as Nullable (Of String)

Now strings are a nullable type but it can't handle:

fn.FormName = dbReader("FormName")

You get an error saying:

Unable to cast object of type 'System.DBNull' to type 'System.String'.

So do I still need to test for null and set to "" or null if this is the
case?

Thanks,

Tom

IIRC you can use .ToString() when returning and indexed column from a
datareader.

i.e.

///////////////
fn.FormName = dbReader("FormName").ToString()
//////////////

should convert that DbNull into a blank string.

Thanks,

Seth Rowe [MVP]
 
IIRC you can use .ToString() when returning and indexed column from a
datareader.

i.e.

///////////////
fn.FormName = dbReader("FormName").ToString()
//////////////

should convert that DbNull into a blank string.

Thanks,

Seth Rowe [MVP]


I don't believe that works.
 
rowe_newsgroups said:
That is odd, since that's what I use in my production code.

Sure it works.

System.DBNull.Value.ToString

returns a zero length string.

ToString is not a type converter, it always returns a string - "A String that
represents the current Object"
 
Back
Top