469,962 Members | 2,347 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.

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 20648
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matt | last post: by
3 posts views Thread by charliewest | last post: by
4 posts views Thread by Jonesgj | 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.