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 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.
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.
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
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
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
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
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.
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
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 :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: tshad |
last post by:
The error I am getting is:
*******************************************************************
Exception Details: System.InvalidCastException: Cast from type 'DBNull' to
type 'String' is not...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| | |