DBNull after read, need to cast

  • Thread starter Thread starter patrick.sannes
  • Start date Start date
P

patrick.sannes

Hi,

I have a question. I have a stored procedure that can give a value
like a string or an int. But in some cases it can be DBNull. What is
the standard way to handle this?
- I can create a storedprocedure that always give the 0 value of the
type.
- Create an if statement around every assignment.

Maybe someone else have an idea?

Patrick
 
Hi,

I have a question. I have a stored procedure that can give a value
like a string or an int. But in some cases it can be DBNull. What is
the standard way to handle this?
- I can create a storedprocedure that always give the 0 value of the
type.
- Create an if statement around every assignment.

Maybe someone else have an idea?

Patrick

At this moment I have this code
if (rdr["MatchBwDepotNr"].GetType() !=
typeof(System.DBNull) )
{
Input.BwplDepotNr =
Convert.ToInt32(rdr["MatchBwDepotNr"]); ;
}
And I think it is quite big if you have to do it for every record....
I hope there is a quicker way to do it.
 
I have a question. I have a stored procedure that can give a value
like a string or an int. But in some cases it can be DBNull. What is
the standard way to handle this?
- I can create a storedprocedure that always give the 0 value of the
type.
- Create an if statement around every assignment.
Maybe someone else have an idea?

At this moment I have this code
if (rdr["MatchBwDepotNr"].GetType() !=
typeof(System.DBNull) )
{
Input.BwplDepotNr =
Convert.ToInt32(rdr["MatchBwDepotNr"]); ;
}
And I think it is quite big if you have to do it for every record....
I hope there is a quicker way to do it.

if(DBNull.Value == rdr["MatchBwDepotNr"])
{
//we are null
}

should do it.

And if you know the column ordinal rdr.IsDBNull(column number) works
too, but who wants to work with ordinals.

framework 2 might allow rdr["MatchBwDepotNr"].IsDBNull, but I i don't
have it on this PC so I can't check.
 
DeveloperX said:
And I think it is quite big if you have to do it for every record....
I hope there is a quicker way to do it.

if(DBNull.Value == rdr["MatchBwDepotNr"])

Or:

if (rdr["MatchBwDepotNr"] is DBNull)

which I think is the neatest version.

(Note that the whole constant==variable idiom isn't required in C#, as
only boolean expressions are valid for "if" statements. Even if I were
to use ==, I'd suggest reversing the operands from your code, to make
it easier to read.)
 
Even if I were
to use ==, I'd suggest reversing the operands from your code, to make
it easier to read.)

You don't subscribe to the catch-inadvertent-assignment-typos-at-
compile-time methodology then, I take it?

cdj
 
Jon said:
DeveloperX said:
And I think it is quite big if you have to do it for every record....
I hope there is a quicker way to do it.
if(DBNull.Value == rdr["MatchBwDepotNr"])

Or:

if (rdr["MatchBwDepotNr"] is DBNull)

which I think is the neatest version.

if(rdr.IsDBNull(ColNoMatchBwDepotNr))

would be my preferred way if column number is an option.

Arne
 
sherifffruitfly said:
You don't subscribe to the catch-inadvertent-assignment-typos-at-
compile-time methodology then, I take it?

Try reversing the operands - it won't compile. C# doesn't have the same
problem as C, because if (foo) will only compile if the type of "foo"
is boolean.
 
Try reversing the operands - it won't compile. C# doesn't have the same
problem as C, because if (foo) will only compile if the type of "foo"
is boolean.

It's force of habit and it does stop me doing if (b = true) :)

Google groups seems to be playing up, so this is the only post I can
read in this thread at the moment, apologies if I've missed something.
 
It's force of habit and it does stop me doing if (b = true) :)

How often do you write that rather than if (b) anyway? I can't
remember the last time I did - and I don't think I've *ever*
accidentally written if (b=true).

To me, it feels obvious that

if (x==5)
is easier to read than
if (5==x)

- it's a more natural way of thinking about things. I would always
say, "Is your name Peter?" rather than "Is Peter your name?" for
instance.

Just because C/C++ is broken enough to make it worth bending
readability for safety doesn't mean it's worth preserving that habit
in saner languages :)
Google groups seems to be playing up, so this is the only post I can
read in this thread at the moment, apologies if I've missed something.

Nah, not a lot :)

Jon
 
How often do you write that rather than if (b) anyway? I can't
remember the last time I did - and I don't think I've *ever*
accidentally written if (b=true).

To me, it feels obvious that

if (x==5)
is easier to read than
if (5==x)

- it's a more natural way of thinking about things. I would always
say, "Is your name Peter?" rather than "Is Peter your name?" for
instance.

Just because C/C++ is broken enough to make it worth bending
readability for safety doesn't mean it's worth preserving that habit
in saner languages :)


Nah, not a lot :)

Jon

Perfectly reasonable point, I would do if(b) too. Well I'll try and
break the habit, but it's been a long time, what's the betting the
first time I do I introduce a fatal bug :)
 

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

Back
Top