472,354 Members | 1,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 10033
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...
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...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
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...

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.