473,411 Members | 2,093 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,411 software developers and data experts.

Null check on executeScalar

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());
}
Oct 12 '06 #1
8 22156
Based purely on the C#:

object value = cmd.ExecuteScalar();
if(value!=null) intContactID = Convert.ToInt32(value);

Marc

Oct 12 '06 #2
Oops; probably need to move the "int" declaration to a separate
location, or add a brace ;-p

Marc

Oct 12 '06 #3
"Earl" <br******@newsgroups.nospamwrote in message
news:Oj**************@TK2MSFTNGP04.phx.gbl...
What's the best way to check for null on an ExecuteScalar? The following
would fire the command twice:
object objValue = cmd.ExecuteScaler();
if (objValue != DbNull.Value)
{
intContactID = Convert.ToInt32(objValue);
}
Oct 12 '06 #4
Thanks Mark and Marc.

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:O3**************@TK2MSFTNGP05.phx.gbl...
"Earl" <br******@newsgroups.nospamwrote in message
news:Oj**************@TK2MSFTNGP04.phx.gbl...
>What's the best way to check for null on an ExecuteScalar? The following
would fire the command twice:

object objValue = cmd.ExecuteScaler();
if (objValue != DbNull.Value)
{
intContactID = Convert.ToInt32(objValue);
}

Oct 12 '06 #5
In case someone else runs across this post, one note to add to your fix.
Must use null instead of DbNull.Value there:

....
if (objValue != null)
....

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:O3**************@TK2MSFTNGP05.phx.gbl...
"Earl" <br******@newsgroups.nospamwrote in message
news:Oj**************@TK2MSFTNGP04.phx.gbl...
>What's the best way to check for null on an ExecuteScalar? The following
would fire the command twice:

object objValue = cmd.ExecuteScaler();
if (objValue != DbNull.Value)
{
intContactID = Convert.ToInt32(objValue);
}

Oct 12 '06 #6
Earl wrote:
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:O3**************@TK2MSFTNGP05.phx.gbl...
>"Earl" <br******@newsgroups.nospamwrote in message
news:Oj**************@TK2MSFTNGP04.phx.gbl...
>>What's the best way to check for null on an ExecuteScalar? The following
would fire the command twice:
object objValue = cmd.ExecuteScaler();
if (objValue != DbNull.Value)
{
intContactID = Convert.ToInt32(objValue);
}
In case someone else runs across this post, one note to add to your fix.
Must use null instead of DbNull.Value there:

...
if (objValue != null)
...
null mean no rows returned.

DbNull.Value means (at least) one row with
(at least) one column but the first row first
column contained a database NULL.

Two completely different scenarios.

Arne
Oct 13 '06 #7
Hmmm, I can't check DbNull.Value of an object though. It has to be the value
of the object -- which it cannot be until I cast it. I can check the object
as to whether or not it is null. So my thinking is, well, ExecuteScalar only
returns one row, one column anyway (a single value) or else it returns null.
Is that the wrong way of looking at it?

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:fyBXg.21466$2g4.15201@dukeread09...
Earl wrote:
>"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:O3**************@TK2MSFTNGP05.phx.gbl...
>>"Earl" <br******@newsgroups.nospamwrote in message
news:Oj**************@TK2MSFTNGP04.phx.gbl...
What's the best way to check for null on an ExecuteScalar? The
following would fire the command twice:
object objValue = cmd.ExecuteScaler();
if (objValue != DbNull.Value)
{
intContactID = Convert.ToInt32(objValue);
}
In case someone else runs across this post, one note to add to your fix.
Must use null instead of DbNull.Value there:

...
if (objValue != null)
...

null mean no rows returned.

DbNull.Value means (at least) one row with
(at least) one column but the first row first
column contained a database NULL.

Two completely different scenarios.

Arne

Oct 13 '06 #8
Earl wrote:
Hmmm, I can't check DbNull.Value of an object though. It has to be the value
of the object -- which it cannot be until I cast it. I can check the object
as to whether or not it is null. So my thinking is, well, ExecuteScalar only
returns one row, one column anyway (a single value) or else it returns null.
Is that the wrong way of looking at it?
The following primitive examples works for me:

using System;
using System.Data.OleDb;

public class MainClass
{
public static void Main(string[] args)
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=C:\\Databases\\MSAccess\\Test.mdb");
con.Open();
OleDbCommand cmd1 = new OleDbCommand("SELECT NULL", con);
object v1 = cmd1.ExecuteScalar();
if(v1 == DBNull.Value)
{
Console.WriteLine("NULL");
}
OleDbCommand cmd2 = new OleDbCommand("SELECT * FROM t1 WHERE 1
2", con);
object v2 = cmd2.ExecuteScalar();
if(v2 == null)
{
Console.WriteLine("null");
}
}
}

Arne
Oct 14 '06 #9

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

Similar topics

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,...
3
by: charliewest | last post by:
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...
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...
4
by: Jonesgj | last post by:
Hi, Whats the best way of capturing a null returned from a query? What through me last night was I knew the table I was querying was empty, so I thought the data reader HasRows method would be...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.