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

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 10107
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James A | last post by:
A particular item in my listbox is null. The field is of type currency. I'm running Access 2000 - SP3 When I use the Isnull(orderitems.column(12,12)) it does not detect it. Also, if i use...
1
by: Matt | last post by:
I could use some help dealing with null blobs. I'm returning a transaction from an Image BLOB field in SQL Server 2000 using C#. If the transaction exists the value is returned with out trouble,...
15
by: TC | last post by:
What does it mean for an integer to have a null value? I am trying to use the DataView.Find method. That method has an integer return type which contains the "index of the row in the DataView...
8
by: Earl | last post by:
What's the best way to check for null on an ExecuteScalar? The following would fire the command twice: if (cmd.ExecuteScalar() != null) { intContactID = Convert.ToInt32(cmd.ExecuteScalar()); }
1
by: js | last post by:
I am using the following C# code and T-SQL to get result object from a SQL Server database. When my application runs, the ExecuteScalar returns "10/24/2006 2:00:00 PM" if inserting a duplicated...
2
by: Manikandan | last post by:
Hi, I have a table with following data Tablename:details No(varchar) Name(varchar) Updated(Datetime) 1 mm 10/10/2006 2 nn 02/12/2005 3 kk NULL I'm using executescalar to get the...
1
by: Manikandan | last post by:
Hi, I have a table with following data Tablename:details No(varchar) Name(varchar) Updated(Datetime) 1 mm 10/10/2006 2 nn 02/12/2005 3 kk NULL I'm using executescalar to get the...
3
by: bogdan | last post by:
Hi, I have a stored procedure that returns a single value. Example: SELECT @RowCount = COUNT(*) FROM t WHERE RETURN @RowCount I created a data set, table adapter, and adapter's method...
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.