Solved

Check for null value in dataset before assignment

Posted on 2007-11-25
8
705 Views
Last Modified: 2008-02-01
Hello, I'm trying to check for a null value before populating an imageUrl for a hyperlinks that I'm adding to a calendar control. How can this be accomplished?

I'm getting the following error:
Conversion from type 'DBNull' to type 'String' is not valid.

Here is my code:

 If Not dsEvents Is Nothing Then
            For Each dr As DataRow In dsEvents.Tables(0).Rows
                nextDate = CType(dr("Date"), DateTime)
                If nextDate = e.Day.Date Then
                    Dim Id As String = CType(dr("Id"), String)
                    Dim hl As New HyperLink
                    Dim lc As New LiteralControl
                    lc.Text = "</BR>"


                    If IsDBNull(dsEvents.Tables(0).Rows(0).Item("ImageURL")) Then
                        hl.ImageUrl = ""
                    Else
                        hl.ImageUrl = ("EventGraphics/" & CType(dr("ImageURL"), String))
                    End If

                    'this works if all the events added contain imageURL
                    'hl.ImageUrl = ("EventGraphics/" & CType(dr("ImageURL"), String))

                    hl.NavigateUrl = ("DisplayEvent.aspx?ID=" & Id)
                    hl.Text = CType(dr("Title"), String)
                    e.Cell.Controls.Add(lc)
                    e.Cell.Controls.Add(hl)
                    e.Cell.Controls.Add(lc)
                End If
            Next
        End If
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
  • 4
  • 4
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20345653
on which line does the error get raised?
0
 

Author Comment

by:gogetsome
ID: 20345674
     hl.ImageUrl = ("EventGraphics/" & CType(dr("ImageURL"), String))
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20345678
what about this:

      If (dsEvents.Tables(0).Rows(0).Item("ImageURL") == DBNull.Value) Then
                        hl.ImageUrl = ""
                    Else
                        hl.ImageUrl = ("EventGraphics/" & CType(dr("ImageURL"), String))
                    End If
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:gogetsome
ID: 20345687
how do you convert c# == to vb?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20345691
sorry, my fault ... I am used to C# :)


      If (dsEvents.Tables(0).Rows(0).Item("ImageURL") IS DBNull.Value) Then
                        hl.ImageUrl = ""
                    Else
                        hl.ImageUrl = ("EventGraphics/" & CType(dr("ImageURL"), String))
                    End If
0
 

Author Comment

by:gogetsome
ID: 20345705
Same error.
Some more info. Some of the events have imageURL and some do not.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20345754
any better with this

      If (dr("ImageURL") is DBNull.Value) Then
                        hl.ImageUrl = ""
                    Else
                        hl.ImageUrl = ("EventGraphics/" & CType(dr("ImageURL"), String))
                    End If
0
 

Author Comment

by:gogetsome
ID: 20345760
Success!
Thank you AngelIII!
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 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