472,370 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,370 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 21605
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.