Solved

Hide image control if no imageurl is returned from DB

Posted on 2007-11-25
3
685 Views
Last Modified: 2013-11-06
Hello, I have used the following code in the past to hide an image control if the ImageURL is null but for some reason it is not working in this situation. Why can't this work?

I'm getting the following error on this line:
   If CType(Picture, String) Is DBNull.Value Then

Conversion from type 'DBNull' to type 'String' is not valid.


Markup:
<asp:Image ID="image" runat="server" ImageUrl= '<%# "~/EventGraphics/" & Eval("ImageURL") %>'
                    Visible='<%# CheckImage(Container.DataItem("ImageURL")) %>'/>


Function:

 Public Function CheckImage(ByVal Picture As Object) As Boolean

        If CType(Picture, String) Is DBNull.Value Then
            Return False
        Else
            Return True
        End If
    End Function
0
Comment
Question by:gogetsome
3 Comments
 
LVL 14

Accepted Solution

by:
steveberzins earned 500 total points
ID: 20348213
I think you want to use this:

 If Picture = DBNull.Value Then
        Return False
        Else
            Return True
        End If

not 100% sure, but think the problem is CType is going to make a string, and the compiler knows that there is not a string equivalent of DBNull.Value, so this will never be true. I've not done a lot of vb.net, but usually the Is keyword used for comparing types, not values, and usually used to do something like if x Is Nothing, or if x is String, not if x is String.Empty. maybe someone can explain this better, but I don't think 'Is' is ever really appropriate for value comparisons. (unless you happen to be a popular past president, then is has a whole different meaning :)
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20348494
<asp:Image ID="image" runat="server" ImageUrl= '<%# "~/EventGraphics/" & Eval("ImageURL") %>'
                    Visible='<%# CheckImage(Eval("ImageURL")) %>'/>



Public Function CheckImage(ByVal Picture As String) As String

        If  Picture <> "" Then
               
            Return  "True"
else
 Return  "False"

                   
        End If
    End Function
0
 

Author Comment

by:gogetsome
ID: 20350083
LOL, I must be a past president..

This did not work:
 If Picture = DBNull.Value Then
        Return False
        Else
            Return True
        End If

But this did:
 If Picture Is DBNull.Value Then
            Return False
        Else
            Return True
        End If

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question