String and null vs DBNULL

T

tshad

I have a function that is being passed as an object but is really a string
so that I can handle DBNull.Value.

You cannot pass a DBNull.Value to a string parameter but you can pass a null
value. Not sure why this is but if I have the following:

private static void SetAddressFields(Address adress, object zip)

where Address is class which has a string property of zip (address.Zip).

In the routing I have the following code:

if (zip != DBNull.Value)
address.Zip = (string)zip;
else
address.FieldToNull("Zip");

Where address.FieldToNull handles null entries.

What is confusing is that if I do:

SetAddressFields(address, dr["Zip"]);

And dr["Zip"] is equal to DBNull.Zip,

my test works: if (zip != DBNull.Value).

If I call the same function where I pass a null where zip is equal to null:

SetAddressFields(address, zip)

or

SetAddressFields(address, null);

Both of these pass back a false from the DBNull.Value test because null is
not DBNull.Value test.

But the code still works.

You can set a string to null, but not to DBNull.Value.

I thought that I would have to change the code to:

if (zip != DBNull.Value && zip != null)

To handle both situations but since you can set a string to null, you only
have to handle the DBNull situation.

Is there another way to handle both situations?

Thanks,

Tom
 
J

jp2msft

Perhaps.

Have you looked at this?

if (String.IsNullOrEmpty(zip) == false)
{
// your code here
}
 
J

jp2msft

....also, System.DbNull is a class, so the types are not the same. Meaning,
you'd have to cast your object to the appropriate type, but once you cast it
to a DbNull, I'm betting there's no reason to test to see if it is DbNull,
right?
 
T

tshad

jp2msft said:
Perhaps.

Have you looked at this?

if (String.IsNullOrEmpty(zip) == false)
{
// your code here
}
But would String.IsNullOrEmpty handle both "null" and DBNull.Value?

Thanks,

Tom
tshad said:
I have a function that is being passed as an object but is really a
string
so that I can handle DBNull.Value.

You cannot pass a DBNull.Value to a string parameter but you can pass a
null
value. Not sure why this is but if I have the following:

private static void SetAddressFields(Address adress, object zip)

where Address is class which has a string property of zip (address.Zip).

In the routing I have the following code:

if (zip != DBNull.Value)
address.Zip = (string)zip;
else
address.FieldToNull("Zip");

Where address.FieldToNull handles null entries.

What is confusing is that if I do:

SetAddressFields(address, dr["Zip"]);

And dr["Zip"] is equal to DBNull.Zip,

my test works: if (zip != DBNull.Value).

If I call the same function where I pass a null where zip is equal to
null:

SetAddressFields(address, zip)

or

SetAddressFields(address, null);

Both of these pass back a false from the DBNull.Value test because null
is
not DBNull.Value test.

But the code still works.

You can set a string to null, but not to DBNull.Value.

I thought that I would have to change the code to:

if (zip != DBNull.Value && zip != null)

To handle both situations but since you can set a string to null, you
only
have to handle the DBNull situation.

Is there another way to handle both situations?

Thanks,

Tom
 
J

jp2msft

No, but...

Again, DbNull is a class that is defined to be null.

I don't understand what you are asking. DbNull does not have a Value property.

Have you created your own DbNull class? If you are using the standard
System.DbNull, it does not have any properties.

Perhaps you could right-click on your DbNull and click Go To Definition to
help me better understand your question.

tshad said:
jp2msft said:
Perhaps.

Have you looked at this?

if (String.IsNullOrEmpty(zip) == false)
{
// your code here
}
But would String.IsNullOrEmpty handle both "null" and DBNull.Value?

Thanks,

Tom
tshad said:
I have a function that is being passed as an object but is really a
string
so that I can handle DBNull.Value.

You cannot pass a DBNull.Value to a string parameter but you can pass a
null
value. Not sure why this is but if I have the following:

private static void SetAddressFields(Address adress, object zip)

where Address is class which has a string property of zip (address.Zip).

In the routing I have the following code:

if (zip != DBNull.Value)
address.Zip = (string)zip;
else
address.FieldToNull("Zip");

Where address.FieldToNull handles null entries.

What is confusing is that if I do:

SetAddressFields(address, dr["Zip"]);

And dr["Zip"] is equal to DBNull.Zip,

my test works: if (zip != DBNull.Value).

If I call the same function where I pass a null where zip is equal to
null:

SetAddressFields(address, zip)

or

SetAddressFields(address, null);

Both of these pass back a false from the DBNull.Value test because null
is
not DBNull.Value test.

But the code still works.

You can set a string to null, but not to DBNull.Value.

I thought that I would have to change the code to:

if (zip != DBNull.Value && zip != null)

To handle both situations but since you can set a string to null, you
only
have to handle the DBNull situation.

Is there another way to handle both situations?

Thanks,

Tom
 
F

Family Tree Mike

jp2msft said:
No, but...

Again, DbNull is a class that is defined to be null.

I don't understand what you are asking. DbNull does not have a Value
property.

Have you created your own DbNull class? If you are using the standard
System.DbNull, it does not have any properties.

Perhaps you could right-click on your DbNull and click Go To Definition to
help me better understand your question.


Actually, the class DBNull has a static field called Value.

A way to use it is shown in MSDN at
http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top