Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for Null Values 1

Status
Not open for further replies.

Zarcom

Programmer
May 7, 2002
1,275
CA
Hey guys
I am not really sure why this isn't working. I am running through my dataset and on a couple of values I put the isdbNull() check. When I do this however, I am getting the following erorr which is rather confusing.
Run-time exception thrown : System.Data.StrongTypingException - Cannot get value because it is DBNull.

If I simply try to access that value with no check then I get this error.
<error: an exception of type: {System.Data.StrongTypingException} occurred>

I ran the select statment through Query Analyzer and got a value of NULL in the problem field. It was my understanding that isDbNull was supposed to check for that value but it doesn't seem to be working much.

<joking>
I feel like I am falling from the pedistal that some of you have put me on (IZY)
</joking>

But your help/ideas/life experiences are appreciated.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Do you really care if the value comes back as null? I mean, are you using it as a check for anything? Reason I ask is because I get around that by using:

Code:
ISNULL(fieldname, '')

in my sprocs or sql statements so that I just get a value of &quot;&quot; rather than worry about dealing with nulls.

Just a thought.
 
Thanks Gagz
That solution does work although it still doesn't explain why the IsDbNull() check wasn't working.
If anyone has found other situations where the isdbnull doesn't work please post. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
You could always declare a variable as Null and then check to see the field you are getting back matches it, if not then do your calc or whatever.

dim DBNull As Object = Convert.DBNull


If Not (dsResults.Tables(0).Rows(0)(&quot;value&quot;) Is DBNull) Then
MyPercent= (dsResults.Tables(0).Rows(0)(&quot;value&quot;)/noofitems)*100
Else
MyPercent=0
End If

This worked for solving a problem i had trying to calc a percentage on a returned value, which when null caused a nasty little error


(The more ASP.NET I do, the more Javascript I find myself using)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top