The IIf
function isn't behaving how you think it does. It's not a ternary-style operator a la C++ or C#. It's just a normal function, which means both your conditional check and the searchstr.Replace are being evaluated before passing it to IIf
. If searchstr is null, searchstr.Replace(...) is going to throw a NullReferenceException, even though you expected it wouldn't be called at all.
The If operator was added in VS2008 to support this, but if you're in 2005, you have to expand it to an If/Else block:
If Not searchstr Is Nothing AndAlso searchstr.Length > 0 Then
searchstr = searchstr.Replace("Desig_id", "designation_id")
Else
searchstr = ""
End If