Reputation: 726
I have the following function:
Public Shared Function imageExists(ByVal path As Object) As Boolean
If IsDBNull(path) = False Or Not path Is Nothing Then
Dim pathString As String = Convert.ToString(path)
If Exists(HttpContext.Current.Server.MapPath(path)) Then
Return True
Else
Return False
End If
Else
Return False
End If
End Function
Called by the visible property for this image control:
<asp:Image ID="img_SmallImage" runat="server" ImageUrl='<%# "~/Content/Images/Exclusive/" + Eval("trip_SmallImage") %>' Visible='<%# OnTheMain.Images.Validation.imageExists(Eval("trip_SmallImage"))%>' />
No matter what I try for the If IsDBNull
part, it either ignores it and executes the code, or returns an error such as Conversion from type 'DBNull' to type 'String' is not valid.
How can I rectify the situation?
Upvotes: 0
Views: 377
Reputation: 1468
Use
If Not String.IsNullOrEmpty(path) Then
as you need to handle empty strings. You should be passing a String, not a generic Object. Also, you can't do Or Not Nothing
. You would have to do Not IsDBNull(path) And Not path Is Nothing
.
Upvotes: 0
Reputation: 9084
I don't know why you are using both IsDBNull
and Nothing
, anyway
Use AndAlso, it will not check the next part if previous is false
check the path first to prevent the exception:
If path IsNot Nothing AndAlso IsDBNull(path) = False Then
'do what ever you want to do
End If
FYI, OrElse will do the same function for Or
, for me i am using this short-circuiting logical operators by default read my question on this here
Upvotes: 1
Reputation: 14565
you have a logical error in the first if check.
Not Nothing
always evaluates to true. so that first statement basically compiles down to:
If IsDBNull(path) Or True Then
which will always be true. your if statement should probably look like this:
If Not IsDBNull(path) And path Is Not Nothing Then
Upvotes: 2