Solved

Check for null value in dataset before assignment

Posted on 2007-11-25
8
700 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]
Comment Utility
on which line does the error get raised?
0
 

Author Comment

by:gogetsome
Comment Utility
     hl.ImageUrl = ("EventGraphics/" & CType(dr("ImageURL"), String))
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
how do you convert c# == to vb?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Success!
Thank you AngelIII!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

10 Experts available now in Live!

Get 1:1 Help Now