Solved

Check for null value in dataset before assignment

Posted on 2007-11-25
8
703 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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