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
odonovanpmAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
What is the exception that you are getting?

Bob
odonovanpmAuthor Commented:
type DBNull cannot be converted to type string
TimCotteeHead of Software ServicesCommented:
Hi odonovanpm,

Use DBNull.Value instead of System.DBNull

Tim Cottee
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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
odonovanpmAuthor Commented:
I am still getting the follow exception

System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid.
TimCotteeHead of Software ServicesCommented:
odonovanpm,

Should be:

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


Tim
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).
Bob LearnedCommented:
What method are you getting the exception from?  Is it from the class that was generated for the strong-type DataSet?

Bob
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PockyMasterCommented:

How are you debugging?
Can you step into the data class?
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?
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
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.
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
odonovanpmAuthor Commented:
Thanks for your help, here are the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.