473,396 Members | 2,036 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,396 software developers and data experts.

Testing for null

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 I run the following:
************************************************** **********
dbReader = myDbObject.RunProcedure("GetNewHire", parameters);
if (dbReader.Read())
{
if (dbReader["SSN"] == null)
ssn = "SSN is equal null";
else
ssn = "SSN is not equal null";
if(dbReader["FirstName"] != null)firstName = (string)dbReader["FirstName"];
if(dbReader["LastName"] != null)lastName = (string)dbReader["LastName"];
if(dbReader["MiddleInitial"] != null)middleInitial =
(string)dbReader["MiddleInitial"];

// if(dbReader["SSN"] != null)ssn = (string)dbReader["SSN"];

************************************************** **********

If I run this I get "SSN is not equal null". If I uncomment the last line,
I get an error:

System.InvalidCastException: Specified cast is not valid.
at MyFunctions.NewHire.GetNewHire(String& returnString)
at MyFunctions.NewHire.AddNewHire()
at TestDll.Class1.Main(String[] args) in
c:\vsprojects\testdll\class1.cs:line
24

Am I not doing the test correctly? I also tried using (dbReader["SSN"] !=
null), but got the same results.

Thanks,

Tom
Mar 7 '06 #1
3 1822
You might actually be after DBNull instead?
Try assigning the content of the reader to an "object" and put a watch
on it to see what it is.
But I think DBNull might be what you are looking for.
Null would indicate that dbReader["SSN"] doesn't actually exist in the
reader's collection.
The problem is that it DOES exist, but the value is DBNull.

Hope this helps.

Mar 7 '06 #2
That was it.

Thanks,

Tom
"Steven Nagy" <le*********@hotmail.com> wrote in message
news:11*********************@z34g2000cwc.googlegro ups.com...
You might actually be after DBNull instead?
Try assigning the content of the reader to an "object" and put a watch
on it to see what it is.
But I think DBNull might be what you are looking for.
Null would indicate that dbReader["SSN"] doesn't actually exist in the
reader's collection.
The problem is that it DOES exist, but the value is DBNull.

Hope this helps.

Mar 7 '06 #3
On Mon, 6 Mar 2006 16:12:49 -0800, tshad wrote:
That was it.


So then you can either compare it to DBNull.Value or call
Convert.IsDBNull().
--
Tom Porterfield
Mar 7 '06 #4

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

Similar topics

6
by: Shabam | last post by:
A web application of mine developed using C# + MS SQL runs fine normally. However when I stress test it with a load testing software (using about 60 simultaneous users) some instances start...
7
by: Filips Benoit | last post by:
Hi, TBL_CONTACT_PERSON CNTP_ID (auto) CNTP_LAST_NAME (required = yes) CNTP_FUNCTION (required = no) CNTP_..... (all required = no) FRM_CONTACT_PERSON_ADD_NEW Property DATA ENTRY = YES
19
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like...
6
by: Lance | last post by:
Hi, How do you test for an object existing in C#? C# seems to differentiate between 'null' and non-existance. In other languages I could test as so: if(ex.InnerException.StackTrace != null)...
5
by: Brian | last post by:
Hello all.. Am working on an Air Hockey game... have an table loaded into a picture box. The borders of the table are slightly slanted. Am using hit testing lines with GDI+ to manipulate the...
4
by: EL OSO | last post by:
Hi, let's suppose I have a class A { int x; ... public static bool operator == (A a1 , A a2) { return (a1.x==a2.x); }
3
by: Jesper | last post by:
Hi, I've made a class with the following operator overloads listed below. However, If I test a 'pointer' for an instace, I get an exception that the object is not set to an insstance of an...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
3
by: IanWright | last post by:
I'm currently in the process of writing a class library, and I'm attempting to write as many unit tests as possible to ensure that I can weed out all the mistakes early on. Part of this involves...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.