473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do you handle nullable strings

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
Feb 19 '08 #1
7 13414
"tshad" <ts***@dslextreme.comschrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Feb 19 '08 #2
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

Feb 20 '08 #3
tshad wrote:
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
Feb 20 '08 #4
On Feb 19, 5:24 pm, "tshad" <ts...@dslextreme.comwrote:
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]
Feb 20 '08 #5
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.
Feb 20 '08 #6
On Feb 20, 11:07 am, "Scott M." <s...@nospam.nospamwrote:
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.
That is odd, since that's what I use in my production code.

Thanks,

Seth Rowe [MVP]
Feb 20 '08 #7
rowe_newsgroups wrote:
On Feb 20, 11:07 am, "Scott M." <s...@nospam.nospamwrote:
>>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.

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"

Feb 21 '08 #8

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

Similar topics

2
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
4
by: Mark Rae | last post by:
Hi, In v1.1 I used to simulate nullable datatypes with structs. e.g. public struct NullableString { private readonly String pstrValue; private readonly bool blnHasValue; public...
89
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
12
by: Kim Hellan | last post by:
I come from C# development, but I have to make a .NET WinForm application in C++. I'm having some troubles handling strings in C++, which seems a lot more problematic than in C#. Lets say I want...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
2
by: CharlesL | last post by:
I am trying to handle binary strings in php. I get a binary output initialization vector from mcrypt as such: from mcrypt: $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); This output may have...
1
by: seth | last post by:
Hi: I'm trying to read JSON strings sent from the browser. Here is the scenario: 1. Using YUI tookit 2. sending JSON string from YUI toolkit - using the provided asyncRequest method. **I...
0
by: nightwatch77 | last post by:
Hello, I have recently stumbled upon a problem with serialization of nullable value types in .Net 2.0 and would appreciate any explanation of what's happening. My application is hosted in IIS...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...

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.