• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1527
  • Last Modified:

Strong Typed Dataset DBNULL Problem

I have a strongly typed dataset that is filled correctly by a SQL data adapter. When I test the "Description" column for a System.DBNull type value I still get an error... The loop is part of a procedure that is working prefectly when the description value is taken out.

For Each oItem As dsList.RoomRow In mProperties.Tables("Room").Rows
            oString &= "<p><span class='Features'>" & CheckForNull(oItem.Room) & " | " & CheckForNull(oItem.Dimensions) & "</span><br>"
            oString &= CheckForNull(oItem.Description)
            oString &= "</p>"
Next


    Private Function CheckForNull(ByVal aItem As Object) As String
        If TypeOf (aItem) Is System.DBNull Then
            Return ""
        Else
            Return CType(aItem, String)
        End If
    End Function
0
odonovanpm
Asked:
odonovanpm
  • 6
  • 6
  • 2
  • +1
1 Solution
 
Bob LearnedCommented:
What is the exception that you are getting?

Bob
0
 
odonovanpmAuthor Commented:
type DBNull cannot be converted to type string
0
 
TimCotteeCommented:
Hi odonovanpm,

Use DBNull.Value instead of System.DBNull

Tim Cottee
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Bob LearnedCommented:
Try this:

For Each oItem As dsList.RoomRow In mProperties.Tables("Room").Rows
            oString &= "<p><span class='Features'>" & oItem.Room.ToString() & " | " & oItem.Dimensions.ToString() & "</span><br>"
            oString &= oItem.Description.ToString()
            oString &= "</p>"
Next oItem

Bob
0
 
odonovanpmAuthor Commented:
I am still getting the follow exception

System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid.
0
 
TimCotteeCommented:
odonovanpm,

Should be:

If aItem Is DBNull.Value Then
    Return ""
Else
    Return CType(aItem,String)
End If


Tim
0
 
odonovanpmAuthor Commented:
No, that's not the problem... The CheckForNull function is not executed - as soon as the Description column is referenced the application throws the exception (unless values are assigned).
0
 
Bob LearnedCommented:
What method are you getting the exception from?  Is it from the class that was generated for the strong-type DataSet?

Bob
0
 
Bob LearnedCommented:
Here is the code that generates the exception:

        Public Property Asker As String
            Get
                Try
                    Return CType(Me(Me.tableQuestions.AskerColumn),String)
                Catch e As InvalidCastException
                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)
                End Try
            End Get
            Set
                Me(Me.tableQuestions.AskerColumn) = value
            End Set
        End Property

If you are not regenerating the DataSet all the time, then you do something like this:

        Public Property Asker As String
            Get
               Return Me(Me.tableQuestions.AskerColumn.ToString()
            End Get
            Set
                Me(Me.tableQuestions.AskerColumn) = value
            End Set
        End Property

I realize that this is a lot of work, and you probably wouldn't want to do it, since it would get lost if you regenerated the DataSet again.

Bob
0
 
PockyMasterCommented:

How are you debugging?
Can you step into the data class?
0
 
odonovanpmAuthor Commented:
TheLearnedOne - you are on to something there but it still appears to be throwing an error... But why can't I test for a null value as normal?
0
 
Bob LearnedCommented:
The DataSet.vb class gets to the exception before you can test it.  That is why I said that you could remove the Try...Catch block, but that's kind of a pain in the ass.

Bob
0
 
odonovanpmAuthor Commented:
I'll try that and see if it works... I have it working at the moment by implicitly referencing each column using oItem("ColumnName") as the object that is past to CheckForNull.

Is the problem you described a flaw in Microsofts development environment? It makes no sense to treat values in this way.
0
 
Bob LearnedCommented:
It sure sounds like a flaw to me, because I use null data all the time, since it means different then empty in my world.

Bob
0
 
odonovanpmAuthor Commented:
Thanks for your help, here are the points.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now