IIf method does not work with IsDBNull(Guid.empty)

492 views Asked by At

For example,

Dim Test as Guid 

Test = IIf(IsDBNull(DataReader("ID")), Guid.Empty, DataReader("ID"))

They caused an error - "Unrecognized guid format"

I was trying to do if statement below

If(IsDBull(DataReader("ID")) Then
   Test = Guid.Emtpy
Else
   Test = DataReader("ID")
End If

It worked for me.

Does 4.0 framework have an issue with IIf method handling empty guid value or non-empty guid value?

2

There are 2 answers

0
Sajed On

logically your codes is same and should work in both way. yes i think its a issue in version 4.0 but you try in this way also

Test = IIf(IsDBNull(DataReader("ID"))=true, Guid.Empty, DataReader("ID"))
0
Major Byte On

The IIf function does not use short-circuit evaluation, it always evaluates all three of its arguments. Which means that even if IsDBNull(DataReader("ID")) equates to true, DataReader("ID") is still evaluated and the probable cause for your error.

This has at least been the case since VB.Net came around so it's not a framework issue.