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

DBNull after read, need to cast

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

Mar 5 '07 #1
9 2623
On 5 mrt, 17:12, patrick.san...@gmail.com wrote:
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.

Mar 5 '07 #2
On 5 Mar, 16:18, patrick.san...@gmail.com wrote:
On 5 mrt, 17:12, patrick.san...@gmail.com wrote:
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.
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.

Mar 5 '07 #3
DeveloperX <nn*****@operamail.comwrote:
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.)

--
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
Mar 5 '07 #4
On Mar 5, 2:34 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
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

Mar 6 '07 #5
Jon Skeet [C# MVP] wrote:
DeveloperX <nn*****@operamail.comwrote:
>>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
Mar 6 '07 #6
sherifffruitfly <sh*************@gmail.comwrote:
On Mar 5, 2:34 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
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?
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.

--
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
Mar 6 '07 #7
On 6 Mar, 07:13, Jon Skeet [C# MVP] <s...@pobox.comwrote:
sherifffruitfly <sherifffruit...@gmail.comwrote:
On Mar 5, 2:34 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
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?

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.

--
Jon Skeet - <s...@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
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.

Mar 6 '07 #8
On Mar 6, 1:15 pm, "DeveloperX" <nntp...@operamail.comwrote:
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

Mar 6 '07 #9
On 6 Mar, 13:50, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Mar 6, 1:15 pm, "DeveloperX" <nntp...@operamail.comwrote:
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
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 :)

Mar 6 '07 #10

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

Similar topics

19
by: jim | last post by:
This line of code returns error 13, cast from 'DBNull' to type 'String' is not valid If IsDBNull(Clinics.Clinics.Item(A).Workphone) The <other code End I Clinics.Clinics is a dataset that...
5
by: DraguVaso | last post by:
Hi, Something I don't understand about a Typed DataSet: When a value in the DataSet is DBNull, it throws this error: "Cannot get value because it is DBNull". But aren't Typed DataSets...
2
by: Filipe Cristóvão | last post by:
Hi, I know that this is a newbie's question, but I need to know how i do to know if a field in a table is DBNull. If I try to write (with response.write) the field, it gives me an error: "Cast...
6
by: tshad | last post by:
The error I am getting is: ******************************************************************* Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not...
4
by: Dursun | last post by:
Hi, I am trying to assign NULL to a datetime field in the SQL Server database. Here is the code that does NOT work: INSERT INTO ... .... VALUES ... .... CType(IIf(dateWitness2Date.Checked,...
8
by: MattB | last post by:
Hello. I have a vb.net (asp.net) application that uses ado.net datasets. At one point, I need to check a text field in a DataTable to see if there's any text in it before performing text operations...
8
by: wink martindale | last post by:
Hello, I am writing a vb.net app.Using MySql db with the corelabs connector. I am getting the following error when attempting to read values from a datareader: Cast from type 'DBNull' to type...
2
by: Julian | last post by:
I have the following code: Dim strQuery As String = "SELECT tblData.CoPayFundGrant, tblData.M_DSS_SSA_Benefits, tblData.O_DSS_SSA_Benefits, " & _ "tblData.Ref_MPS, tblData.Ref_LA,...
4
by: Jose Fernandez | last post by:
Hello. how can i cast a null value from a datareader (System.DBNull) to dateTime I have an object. One of its fields is a DateTime type field. From the database i am getting System.DBNull values...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.