handling dbnulls

G

Guest

hey all,

if (DataReader["ParentId"].ToString() != String.Empty)
{
oCategory.ParentId = Convert.ToInt32(DataReader["ParentId"]);
}

ParentId has some null values coming from my database. Is this the correct
way to handle the nulls for this field (or any field for that matter)?

When i debug, inside the immediate window i type:
?DataReader["ParentId"]
{ }

I'm not sure what the 2 braces mean so i added the ToString() and just
checked for " ".

One more thing, about the Convert.ToInt32 is this ok to use? Why isn't there
just a Convert.ToInteger?

thanks,
rodchar
 
N

Nicholas Paldino [.NET/C# MVP]

rodchar,

This should work, as the documentation for the overload of ToString on
DBNull says that this returns an empty string. However, I think it is
better if you compare against the value, like so:

if (DataReader["ParentId"] != DBNull.Value)
{
...
}

The reason you see empty brackets in the debugger is because the
debugger is calling ToString on the DBNull instance that
DataReader["ParentId"] is returning, so you get no value.

As far as why the method isn't called ToInteger on the convert class,
it's because the type in .NET is Int32, and it is following that convention.
"int" in C# is just an alias for Int32 in the System namespace.

I would recommend casting for the most part. Granted, not all data
adapter types are going to return types that cast to your basic types (int,
string, etc, etc), but any data adapter worth its salt will.
 
J

Jon Skeet [C# MVP]

hey all,

if (DataReader["ParentId"].ToString() != String.Empty)
{
oCategory.ParentId = Convert.ToInt32(DataReader["ParentId"]);
}

ParentId has some null values coming from my database. Is this the correct
way to handle the nulls for this field (or any field for that matter)?

It's not a terribly nice way of doing it. I'd use

if (!(DataReader["ParentId"] is DBNull))
{
....
}
One more thing, about the Convert.ToInt32 is this ok to use? Why isn't there
just a Convert.ToInteger?

Because the .NET type name is ToInt32. "ToInteger" would be ambiguous
between Int16, Int32 and Int64 (aka short, int, long in C#).

Jon
 
M

madhatter2647

if (DataReader["ParentId"] != System.DBNull.Value)

is the correct syntax

the 'two braces' are the identifier for the column in the datareader.

there is no 'convert.tointeger' function because there are many types
of integer representations in the framework. Int16, Int32, Int64 for
signed integers (ie negative and positive numbers) and UInt32, which
is an unsigned integer (only positive numbers)
 
M

Mark Rae [MVP]

It's not a terribly nice way of doing it. I'd use

if (!(DataReader["ParentId"] is DBNull))
{
...
}

Is there any appreciable difference between the above and:

if (DataReader["ParentId"] != DBNull.Value)
{
....
}
 
J

Jon Skeet [C# MVP]

Jon Skeet said:
It's not a terribly nice way of doing it. I'd use
if (!(DataReader["ParentId"] is DBNull))
{
...
}

Is there any appreciable difference between the above and:

if (DataReader["ParentId"] != DBNull.Value)
{
...

}

Well, when testing in a "positive" way I find

if (DataReader["ParentId"] is DBNull)

more readable than

if (DataReader["ParentId"] == DBNull.Value)

and I was just trying to be consistent with that :)

Jon
 
M

Mark Rae [MVP]

Well, when testing in a "positive" way I find

if (DataReader["ParentId"] is DBNull)

more readable than

if (DataReader["ParentId"] == DBNull.Value)

and I was just trying to be consistent with that :)

Fair enough.

BTW, this wasn't a criticism - I was just curious if "is" was more efficient
than "=="...
 
J

Jon Skeet [C# MVP]

Mark Rae said:
Well, when testing in a "positive" way I find

if (DataReader["ParentId"] is DBNull)

more readable than

if (DataReader["ParentId"] == DBNull.Value)

and I was just trying to be consistent with that :)

Fair enough.

BTW, this wasn't a criticism - I was just curious if "is" was more efficient
than "=="...

Sure - I wasn't taking it as a criticism.

I've no idea which is more performant to be honest - but I doubt that
it'll be the bottleneck in most code anyway, so I'd go with whatever's
most readable :)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Jon Skeet said:
It's not a terribly nice way of doing it. I'd use
if (!(DataReader["ParentId"] is DBNull))
{
...
}

Is there any appreciable difference between the above and:

if (DataReader["ParentId"] != DBNull.Value)
{
...

}

Well, when testing in a "positive" way I find

if (DataReader["ParentId"] is DBNull)

more readable than

if (DataReader["ParentId"] == DBNull.Value)

and I was just trying to be consistent with that :)

I like the latter more :)

It should be that I live in the tropics and you live in England :)
 

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