473,668 Members | 2,821 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("FormN ame")

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 13447
"tshad" <ts***@dslextre me.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("FormN ame")

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(dbRe ader(...), 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("FormN ame")

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(dbFiel dName, '') to get the DB to convert nulls to empty strings.

Andrew
Feb 20 '08 #4
On Feb 19, 5:24 pm, "tshad" <ts...@dslextre me.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("FormN ame")

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("FormN ame").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("FormN ame").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.no spamwrote:
IIRC you can use .ToString() when returning and indexed column from a
datareader.
i.e.
///////////////
fn.FormName = dbReader("FormN ame").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.no spamwrote:
>>IIRC you can use .ToString() when returning and indexed column from
a datareader.
>>i.e.
>>///////////////
fn.FormName = dbReader("FormN ame").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.V alue.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
2619
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 feasible. It would be helpful if people could test the patched Python with their own applications and report any incompatibilities. PEP: 349
4
5874
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 NullableString(String strValue) {
89
5119
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 the contents of pString2 to pString? (giving "Blah Blah Blah")
5
51627
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 DataColumn( "StartDate", typeof( DateTime? ) ) ); dtSearchFromData.Columns.Add( new DataColumn( "EndDate", typeof( System.Nullable<DateTime>) ) ); Any ideas?
12
4384
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 to construct a string like this which combines string variables, plain strings and strings from WinForm controls: string startStr = "<just starting>"; string newStr = startStr + myControl1->Text + "getting further" + myControl2->Text + "the...
8
10700
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
2731
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 padded nulls at the end which are significant. I would like to convert all characters including all padding nulls at end and convert it in a string to be saved into a database in such a way
1
2290
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 would like to read the JSON string sent from the client on the
0
1552
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 and exposes a web service with nullable data types - like: public class DataItem { DateTime? Timestamp; int? SomeData; }
0
8381
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8583
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7401
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5681
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4205
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1786
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.