Solved

Hide image control if no imageurl is returned from DB

Posted on 2007-11-25
3
686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

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