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

G

Guest

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,
 
I

intrader

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top