0

I'm working on a project where I get data from a database.

Now I get this error message:

Conversion from type 'DBNull' to type 'String' is not valid

I know what the error means, I just need a way to fix it.

I have the following code:

 Private Function ParseSeamansNames(ByVal seaman As ItemsDataSet.SeamenRow) As String
        If (seaman Is Nothing) Then Return String.Empty

        Dim name As String
        Dim firstNames() As String = Split(seaman.FirstName.Replace("-", " "))
        Dim index1 As Integer = CInt(seaman.GivenNameNumber.Substring(0, 1))
        Dim index2 As Integer = CInt(seaman.GivenNameNumber.Substring(1, 1))
        If (index1 > firstNames.Length - 1) Then
            index1 = 0
        End If
        If (index2 > firstNames.Length - 1) Then
            index2 = 0
        End If
        If (index1 = 0 And index2 = 0) Then
            name = seaman.FirstName
        ElseIf (index1 > 0 And index2 = 0) Then
            name = firstNames(index1 - 1)
        ElseIf (index1 = 0 And index2 > 0) Then
            name = firstNames(index2 - 1)
        Else
            name = firstNames(index1 - 1) & "-" & firstNames(index2 - 1)
        End If
        name = name & " " & seaman.LastName

        Return name
    End Function

I tried to change it to If (seaman Is Nothing) Then Return DBNull but I get error:

dbnull is a type and cannot be used as an expression

I don't really know how to fix it. Can anyone help me?

UPDATE:

I get this error:

[InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.]
Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object Value) +715847
BUMSSeamenWebb.ItemsService.SeamenRow.get_FirstName() in C:\BUMS LOKAL\Dev\Projects\BUMSSeamenWebb\Web References\ItemsService\Reference.vb:51036

That line is this code:

 <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
             Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")>  _
            Public Property FirstName() As String
                Get
                    Try 
                        Return CType(Me(Me.tableSeamen.FirstNameColumn),String)
                    Catch e As Global.System.InvalidCastException
                        Throw New Global.System.Data.StrongTypingException("The value for column 'FirstName' in table 'Seamen' is DBNull.", e)
                    End Try
                End Get
                Set
                    Me(Me.tableSeamen.FirstNameColumn) = value
                End Set
            End Property

In particular:

 Return CType(Me(Me.tableSeamen.FirstNameColumn),String)

I can't see the problem here

UPDATE 2:

This is another function that checks other columns

 Private Sub SetSeamanData(ByRef itemRow As ItemsDataSet.ItemsRow, ByVal seaman As ItemsDataSet.SeamenRow)
        itemRow.PersonalIdentityNumber = seaman.PersonalIdentityNumber
        itemRow.Name = ParseSeamansNames(seaman)
        itemRow.CitizenShipCode = seaman.CitizenshipCode
        If Not seaman.IsEmailNull() Then
            itemRow.EmailAddress = seaman.Email
        Else
            itemRow.EmailAddress = Nothing
        End If
        If Not seaman.IsTelephoneNull() Then
            itemRow.TelephoneNumber = seaman.Telephone
        Else
            itemRow.TelephoneNumber = Nothing
        End If
        If Not seaman.IsMobiletelephoneNull() Then
            itemRow.MobilephoneNumber = seaman.Mobiletelephone
        Else
            itemRow.MobilephoneNumber = Nothing
        End If
    End Sub
4
  • @sstan Thanks, I updated my question. I appreciate it if you could check it again. Commented Jun 7, 2016 at 21:07
  • If Not IsDBNull then perform your checks...Do keep in mind that DBNull is NOT in fact the same as Nothing Commented Jun 7, 2016 at 21:14
  • Also as a side note, it might be good to check to make sure the values you're casting to INT are in fact numbers before you try and cast them... Commented Jun 7, 2016 at 21:21
  • DataRow type have extension method .Field(Of T), which can be used for String without null checking. Return Nothing if value is DBNull Commented Jun 7, 2016 at 21:50

1 Answer 1

1

These generated DataRow classes are coded in such a way that if you try to read a property (column value) that has a database null value, it will throw an exception.

You are expected to check for null before attempting to read from it.

As you can tell from the message, in this case it's complaining that you are trying to read the value of FirstName, but it's null (DbNull).

Usually, when a column can be null, these DataRow classes also include a convenience function to check for null on that specific column. So in this case, ItemsDataSet.SeamenRow should also have a IsFirstNameNull function. If so, call that first, and handle that specific case. If IsFirstNameNull returns false, then you can safely read from the FirstName property.

If you have other columns that can be null in your database, you will need to follow a similar pattern.

Sign up to request clarification or add additional context in comments.

2 Comments

I do have a IsFirstNameNull function that checks for null, I just never used it I guess. I have another function that checks for null, please check my Update 2. Is the IsFirstNameNull supposed to work in similar way? Thank you.
Yes, exactly. That's the pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.