473,388 Members | 1,340 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,388 software developers and data experts.

handling dbnulls

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
Aug 8 '07 #1
8 4284
On Aug 8, 2:56 pm, rodchar <rodc...@discussions.microsoft.comwrote:
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

Aug 8 '07 #2
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)

Aug 8 '07 #3
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
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)
{
....
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 8 '07 #4
On Aug 8, 3:25 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
"Jon Skeet [C# MVP]" <sk...@pobox.comwrote in messagenews:11**********************@b79g2000hse.g ooglegroups.com...
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

Aug 8 '07 #5
Thanks everyone for this wealth of knowledge. As always, I appreciate it.
Rod.

"rodchar" wrote:
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
Aug 8 '07 #6
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@b79g2000hse.googlegro ups.com...
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 "=="...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 8 '07 #7
Mark Rae [MVP] <ma**@markNOSPAMrae.netwrote:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@b79g2000hse.googlegro ups.com...
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 8 '07 #8
Hi,

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@b79g2000hse.googlegro ups.com...
On Aug 8, 3:25 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
>"Jon Skeet [C# MVP]" <sk...@pobox.comwrote in
messagenews:11**********************@b79g2000hse. googlegroups.com...
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 :)
Aug 8 '07 #9

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

Similar topics

2
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
9
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that...
7
by: yogeshnelwadkar | last post by:
Hello, i have a problem with replacing c++ exception handling with structured exception handling. How to replace the " catch(...) " in c++ exception handling with, __except , a structured...
13
by: Fred Chateau | last post by:
I can't seem to find a test for DBNulls. Whatever I try doesn't work. for (int x = 0; x < dataSet.Identity.Rows.Count; x++) { DataRow dataRow = dataSet.Tables.NewRow(); if...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.