Chacking DBNulls using IIf function

dhj

Regular
Joined
Sep 18, 2003
Messages
82
Location
Sri Lanka
i have a typed dataset, one field is allow null,(say FirstName is allow null)
when i'm retreiving data to the UI i need to check FirstName for null values
so i'm doing this as follow

say my types dataset is ds and oRow is the pointed row

with oRow

txtFirstName.Text=IIF(.IsFirstNameNull,"",.FirstName)
end With


but my problem is this IIF doesn't work propery.

if i did this using if else statements without using IIF it is working properly

pls help me, pls explain me y this IIF is not function properly,this is in my windows application and i'm using vb.net

thank you
 
I really don't know why by maybe if you try to work aroud the evaluation it will work...
Something like:

txtFirstName.Text=IIF(.FirstName = dbnull.value,"",.FirstName)

Try it...
If not... use the If-> Else... How bad could it be?? :cool:

Alex :p
 
Arch4ngel said:
If -> Else and IIF will probably give exactly the same assembly anyway. So if it don't work with IIF then ... If Else

Arch4ngel: That's the logic but as dhj said:
if i did this using if else statements without using IIF it is working properly

Starnge, not logical... but as stated... true...

Anyways...

Joe Mamma's idea seems fine to me as I see it...

Alex :p
 
hi all
thank you very much for all of u for ur comments

i have tried Alex's method
txtFirstName.Text=IIF(.FirstName = dbnull.value,"",.FirstName)
it is giving me a error as "Operator '=' is not defined for types 'String' and 'System.DBNull'. Use 'Is' operator to compare two reference types."

and then i tried Joe Mamma's method
txtFirstName.Text=Convert.ToString(.FirstName)

but still the same problem occures

ofcause as u guys said i can easily go for if.. else ... statement. but imagine if i have plenty of allow null fields, then i need to write if .. else statement for each and every one.

that is y i have selected IIf to do me the job. but still i cannot figure out y this is not functioning propery.

as u guys said IT IS STRANGE.....
 
I once encountered a similar problem, something like:

val = IIF(otherVal Is DBNull.Value, 0, Convert.ToDouble(otherVal))

then I remembered that IIF evaluates BOTH true and false parts of the IIF arguments, regardless if the 1st param is actually true or false, hence the error (btw, the same is not true for C#'s conditional operator ?: ) I guess you really have no choice but to use If...Else...End If blocks; if you're concerned about the resulting verbosity of your code, then you might want to encapsulate this logic in a function.
 
Back
Top