Solved

Check for null value in dataset before assignment

Posted on 2007-11-25
8
702 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:gogetsome
ID: 20345687
how do you convert c# == to vb?
0
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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