473,889 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ExecuteSca lar() != null)
{
intContactID = Convert.ToInt32 (cmd.ExecuteSca lar());
}
Oct 12 '06 #1
8 22381
Based purely on the C#:

object value = cmd.ExecuteScal ar();
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******@newsg roups.nospamwro te in message
news:Oj******** ******@TK2MSFTN GP04.phx.gbl...
What's the best way to check for null on an ExecuteScalar? The following
would fire the command twice:
object objValue = cmd.ExecuteScal er();
if (objValue != DbNull.Value)
{
intContactID = Convert.ToInt32 (objValue);
}
Oct 12 '06 #4
Thanks Mark and Marc.

"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:O3******** ******@TK2MSFTN GP05.phx.gbl...
"Earl" <br******@newsg roups.nospamwro te in message
news:Oj******** ******@TK2MSFTN GP04.phx.gbl...
>What's the best way to check for null on an ExecuteScalar? The following
would fire the command twice:

object objValue = cmd.ExecuteScal er();
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**@markNOSPA Mrae.comwrote in message
news:O3******** ******@TK2MSFTN GP05.phx.gbl...
"Earl" <br******@newsg roups.nospamwro te in message
news:Oj******** ******@TK2MSFTN GP04.phx.gbl...
>What's the best way to check for null on an ExecuteScalar? The following
would fire the command twice:

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

Oct 12 '06 #6
Earl wrote:
"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:O3******** ******@TK2MSFTN GP05.phx.gbl...
>"Earl" <br******@newsg roups.nospamwro te in message
news:Oj******* *******@TK2MSFT NGP04.phx.gbl.. .
>>What's the best way to check for null on an ExecuteScalar? The following
would fire the command twice:
object objValue = cmd.ExecuteScal er();
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.d kwrote in message
news:fyBXg.2146 6$2g4.15201@duk eread09...
Earl wrote:
>"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:O3******* *******@TK2MSFT NGP05.phx.gbl.. .
>>"Earl" <br******@newsg roups.nospamwro te in message
news:Oj****** ********@TK2MSF TNGP04.phx.gbl. ..
What's the best way to check for null on an ExecuteScalar? The
following would fire the command twice:
object objValue = cmd.ExecuteScal er();
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.Ole Db;

public class MainClass
{
public static void Main(string[] args)
{
OleDbConnection con = new
OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
Source=C:\\Data bases\\MSAccess \\Test.mdb");
con.Open();
OleDbCommand cmd1 = new OleDbCommand("S ELECT NULL", con);
object v1 = cmd1.ExecuteSca lar();
if(v1 == DBNull.Value)
{
Console.WriteLi ne("NULL");
}
OleDbCommand cmd2 = new OleDbCommand("S ELECT * FROM t1 WHERE 1
2", con);
object v2 = cmd2.ExecuteSca lar();
if(v2 == null)
{
Console.WriteLi ne("null");
}
}
}

Arne
Oct 14 '06 #9

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

Similar topics

1
3648
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, but because the ID can exist without having a value in the Image column the returned value is NULL and the code can't handle it and I receive this error when the Stored Procedure's value is returned (the line of code is marked with "**HERE**": ...
3
10159
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 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;
15
29291
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 containing the sort key value specified; otherwise a null value if the sort key value does not exist." By "null value", does it mean System.DBNull? (I thought only objects could evaluate to System.DBNull.) How can I test whether an integer variable...
4
1722
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 enough. Hoever, after sleeping on the problem I thought I'd just try my query straight into Query Analyser, and then it dawned on me that my query was a aggregate query. So it returns a a Row with NULL.
1
14529
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 record. It returns null for all other conditions. Does anyone know why? Does anyone know how to get the output value? Thanks. ------ C# ----- aryParams = {'10/24/2006 2pm', '10/26/2006 3pm', 2821077, null}; object oRtnObject = null;...
2
3353
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 updated values from the table
1
5747
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 updated values from the table
3
5009
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 configured for the stored proc and as returning a single value. The wizard created an adapter method that calls SqlCommand.ExecuteScalar(). The problem is that
2
14678
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 can I pass null value as parameter to the database stored procedure programattically using C#? Although I can check for empty column (the following code passes string.Empty as parameter but how to pass null value?), I cannot check for null value...
0
9961
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9803
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11187
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10783
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10887
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9598
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7148
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6021
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.