473,387 Members | 1,863 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,387 software developers and data experts.

Null Returned from SQL Server Query

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.

This was what I was trying to do - avoid nulls

If MyDataread.HasRows Then

MyDatreader = .......(CommandBehavior.Default)

Do While MyDataread.Read
SELECT Max(Rank+1) as MaxRank FROM MyTable
......
Loop

Else
......

Any assistance greatfully accepted.

Thanks

JonesGJ
Nov 21 '05 #1
4 1709
Hello JonesGJ, owl are you?

You wanna wake up to using ExecuteScalar for that kind of SQL not
ExecuteReader.
But dont be alarmed as MSDN has blanketed that subject area with more
helpful
hints than you've had pillow fights.

Im assuming you've em-bed-ded that SQL in a stored proc rather than
just letting it drift about among your code like biscuit crumbs between the
sheets.

hth
Richard

Nov 21 '05 #2
Hi,

Take a look at the datareaders isdbnull property.
http://msdn.microsoft.com/library/de...bnulltopic.asp

http://msdn.microsoft.com/library/de...bnulltopic.asp
Ken
----------------------------
"Jonesgj" <g@btinternet.com> wrote in message
news:co**********@titan.btinternet.com...
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.

This was what I was trying to do - avoid nulls

If MyDataread.HasRows Then

MyDatreader = .......(CommandBehavior.Default)

Do While MyDataread.Read
SELECT Max(Rank+1) as MaxRank FROM MyTable
......
Loop

Else
......

Any assistance greatfully accepted.

Thanks

JonesGJ

Nov 21 '05 #3
Hello Jonesgj,

It seems that looping through the DataReader is a waste in this scenario.
You only get one column and one row after all, so why don't you use ExecuteScalar
which returns an object? I'm not exactly sure what the method is going to
return when the data is NULL, but I suggest that you check if return value
is System.DBNull or null (or Nothing in VB).

Hope it helps.
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.

This was what I was trying to do - avoid nulls

If MyDataread.HasRows Then

MyDatreader = .......(CommandBehavior.Default)

Do While MyDataread.Read
SELECT Max(Rank+1) as MaxRank FROM MyTable
......
Loop
Else
......
Any assistance greatfully accepted.

Thanks

JonesGJ

Nov 21 '05 #4
Thanks Richard ... apologies in the delay in returning this. I solved this
using an isnull in my sql....and ofcourse I use Stored procs where
appropriate ;-)

However, I do struggle in getting values retunred from my procs. Any ideas?

Best regards

JonesGJ
"Richard Myers" <fa**@address.com> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
Hello JonesGJ, owl are you?

You wanna wake up to using ExecuteScalar for that kind of SQL not
ExecuteReader.
But dont be alarmed as MSDN has blanketed that subject area with more
helpful
hints than you've had pillow fights.

Im assuming you've em-bed-ded that SQL in a stored proc rather than
just letting it drift about among your code like biscuit crumbs between the sheets.

hth
Richard

Nov 21 '05 #5

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

Similar topics

3
by: Dimitri Furman | last post by:
SQL Server 2000 SP3. Is it possible for the @@ROWCOUNT function to return NULL after a statement? I am troubleshooting a relatively large stored procedure with multiple SELECT statements and a...
3
by: olanorm | last post by:
I have a query where one or more of the columns returned is a result from a subquery. These columns get their own alias. I want to filter out the rows containing NULL from the subqueries but it...
6
by: AAVF | last post by:
Hi We have a problem with a query. An Access database links via ODBC to a UNIX server. To speed things, we use the ODBC to load the relevant tables to the local PC that runs Access so that...
4
by: Steve Oster | last post by:
Hi all, I have to create a query using MS Query, and what I need is beyond my knowledge. I am querying from a list of invoices, and each invoice can have several delivery dates (possibly a...
1
by: RiesbeckP | last post by:
Hi All, I have a DB where there are customer numbers and a few other fields. I want to be able to pull all of the null records for a particular field as well as all the other customer numbers...
26
by: Martin R | last post by:
Hi, How to find first not null value in column whitout chacking whole table (if there is a not null value then show me it and stop searching, the table is quite big)? thx, Martin *** Sent...
3
by: tshad | last post by:
I have a query that is returning a null and I am testing for null. The value being sent back is a varChar(11) from Sql Server. If I do the query from Query Analyser I get NULL returned. But if...
3
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero...
0
by: Algobardo | last post by:
Good morning, this is the first time i write on this forum because i googled and i've seen related post with no solution. I will expose briefly the problem. I'm using c# 3.5 and i'm trying using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.