Solved

Check for null value in dataset before assignment

Posted on 2007-11-25
8
701 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
  • 4
  • 4
8 Comments
 
LVL 142

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 142

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
 

Author Comment

by:gogetsome
ID: 20345687
how do you convert c# == to vb?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 142

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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

27 Experts available now in Live!

Get 1:1 Help Now