I am trying to write a generic function that will check each database parameter to see if it is null, and if so, return DBNull; if not, return the object.
So here is my function:
Public Shared Function CheckForNull(ByVal obj As Object) As Object
If obj <> Nothing Then Return obj Else Return DBNull.Value
End Function
My problem is that some of the objects I am passing to the function are Nullable. So I may pass the function a Long? or Int?, but when a nullable type is passed to the function, it is converted to its value type. So if I pass a Long? that has a value of 0, the function returns DBNull because the Long? is converted to Long and a value of 0 for a Long is equivalent to Nothing. Is there anyway to make this function work for Nullable types as well?
If not, I will just fall back to using the following statements instead of one generic funciton call:
IIf(nullableVar.HasValue, nullableVar, DBNull.Value))
and
IIf(nonNullableVar IsNot Nothing , nonNullableVar, DBNull.Value))