469,962 Members | 2,375 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,962 developers. It's quick & easy.

How to detect NULL value with cmd.ExecuteScalar();

Hi -

I need to detect when the ExecuteScalar() method of the cmd object returns
NULL. I have tried the below code, however, it always returns false (this is
to say, that ExecuteScalar never returns NULL when in fact it does):

cmd.CommandText = "...SQL String goes here...";
cmd.CommandType = CommandType.Text;
if (cmd.ExecuteScalar() == System.DBNull.Value)
iScorecardId = 1;
else
iScorecardId = Convert.ToInt32(cmd.ExecuteScalar()) + 1;

Is there another way to check if ExecuteScalar returns NULL?

Thanks,
Nov 19 '05 #1
3 9905
On Wed, 06 Jul 2005 09:35:12 -0700, charliewest wrote:
Hi -

I need to detect when the ExecuteScalar() method of the cmd object returns
NULL. I have tried the below code, however, it always returns false (this is
to say, that ExecuteScalar never returns NULL when in fact it does):

cmd.CommandText = "...SQL String goes here...";
cmd.CommandType = CommandType.Text;
if (cmd.ExecuteScalar() == System.DBNull.Value)
iScorecardId = 1;
else
iScorecardId = Convert.ToInt32(cmd.ExecuteScalar()) + 1;

Is there another way to check if ExecuteScalar returns NULL?

Thanks,

try IsDbNull

Nov 19 '05 #2
Do you know what the equivalent of the *IsDBNull* function is for C#?

Thanks,

"intrader" wrote:
On Wed, 06 Jul 2005 09:35:12 -0700, charliewest wrote:
Hi -

I need to detect when the ExecuteScalar() method of the cmd object returns
NULL. I have tried the below code, however, it always returns false (this is
to say, that ExecuteScalar never returns NULL when in fact it does):

cmd.CommandText = "...SQL String goes here...";
cmd.CommandType = CommandType.Text;
if (cmd.ExecuteScalar() == System.DBNull.Value)
iScorecardId = 1;
else
iScorecardId = Convert.ToInt32(cmd.ExecuteScalar()) + 1;

Is there another way to check if ExecuteScalar returns NULL?

Thanks,

try IsDbNull

Nov 19 '05 #3
charliewest wrote:
Do you know what the equivalent of the *IsDBNull* function is for C#?


val == DbNull.Value

or

val is DbNull
Hans Kesting
Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matt | last post: by
8 posts views Thread by Earl | last post: by
1 post views Thread by js | last post: by
2 posts views Thread by Manikandan | last post: by
1 post views Thread by Manikandan | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.