Solved

Hide image control if no imageurl is returned from DB

Posted on 2007-11-25
3
681 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deletiā€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now