Saturday, March 24, 2012

Operator <> is not defined for type DBNull and string ""

HI!

I'm trying to load an image when parsing through records, only i get the error:

Operator '<>' is not defined for type 'DBNull' and string ""

here's the code:

PrivateSub LoadData(ByVal dataAs DataRow)

If data.Item(11) <>""Then

Image1.ImageUrl ="\\sacavem07:6065\images\" & data.Item(11)

EndIf

EndSub

The error happens on theIf data.Item(11) <>""Then line.

what's wrong here?

try something like this:
If you wanna test for not equal to empty string you can do a nested If inside this one--your DB has a value of NULL i believe that's why your getting the error.

If row.Item(5)Is System.DBNull.Value =TrueThen

'do something

End If


Amd if you are using 2.0 (which you should be!), there's a great new part of the String class

If Not String.IsNullOrEmtpty(data.Item(11)) then
Image1.ImageUrl = "\\sacavem07:6065\images\" & data.Item(11)
End If

If you are still stuck in 1.1, then

If Not IsDBNull(data.Item(11)) AndAlso data.Item(11) <> "" then
Image1.ImageUrl = "\\sacavem07:6065\images\" & data.Item(11)
End If

The "AndAlso" in that if statement short circuits the if it fails on the left side...

so if you had

if A and B then

even if A evaluated to false, it would still evaluate B

if A AndAlso B then

if A was false, the code stops there... this AndAlso was a great great addition to 1.1


i did it this way:

If data.Item(11)Is System.DBNull.Value =FalseThen

Image1.ImageUrl ="\\sacavem07:6065\images\" & data.Item(11)

EndIf

but it doesn't show any image. on the DB the image field is a char with the name of the file, like this: pic1.jpg

shouldn't it load the picture like this?


No, that wouldnt work as if you use "Is" then you wouldn't use equals..

Just take a shot at using what i posted instead of wasting time guessing..... i can tell you my code does work as i do checks like that all the time


The code he placed is code I use all the time for checks as well. But then again, I don't use images of any sort in my DB's so I'm not to sure what would happen then.

I tried your code morningZ, but it gives an error. it underlines

String.IsNullOrEmtpty

and the error is:

IsNullOrEmtpty is not a member of 'string'.

i'm programming is VB.net i don't know if that has any influence on this code snippet you gave me.


since mine didn't work--you placed the asp.net 2.0 code in there --is that what your running? if not try the other portion he wrote--that was for 1.1

i'm using 2.0

and your code didn't give me any errors. it just doesn't show the image. is it necessary to put some autopostback or something for the image to appear? cause if it shows the other data the way i'm doing it, why doesn't it show the pic? do i need to do anything special?

i've tried the code for 1.1 and it doesn't return errors but it doesn't show my pic too.

IfNot IsDBNull(data.Item(11))AndAlso data.Item(11) <>""Then

Image1.ImageUrl ="http://sacavem07:6065/images/" & data.Item(11)

EndIf

is there another way to load a pic from the DB? where the pic field in the DB is a char and only has the name of the pic file?


If Not (data.Item(11) Is System.DBNull.Value) AndAlso data.Item(11).ToString().Length > 0 Then

Using stringValue.Length > 0 is always faster and more efficient than stringValue <> "".

NC...


data.Item(11) <> "" is not going to work as data.Item(11) is an object and not a string, and you're trying to use a string comparison.

NC...


suu wrote:

String.IsNullOrEmtpty

and the error is:

IsNullOrEmtpty is not a member of 'string'.

then you are not doing something right....

http://www.google.com/search?complete=1&hl=en&q=string.isnullorempty

ah well... good luck....


Probably because he's either not using 2.0 or "Emtpty" is misspelled (should be "Empty").

NC...

0 comments:

Post a Comment