title: Error: Conversion from type 'DBNull' to type 'String' is not valid
body: <p>I'm working on a project where I get data from a database. </p>
  
  <p>Now I get this error message:</p>
  
  <blockquote>
    <p>Conversion from type 'DBNull' to type 'String' is not valid</p>
  </blockquote>
  
  <p>I know what the error means, I just need a way to fix it.</p>
  
  <p>I have the following code:</p>
  
  <pre><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 &gt; firstNames.Length - 1) Then
              index1 = 0
          End If
          If (index2 &gt; firstNames.Length - 1) Then
              index2 = 0
          End If
          If (index1 = 0 And index2 = 0) Then
              name = seaman.FirstName
          ElseIf (index1 &gt; 0 And index2 = 0) Then
              name = firstNames(index1 - 1)
          ElseIf (index1 = 0 And index2 &gt; 0) Then
              name = firstNames(index2 - 1)
          Else
              name = firstNames(index1 - 1) &amp; "-" &amp; firstNames(index2 - 1)
          End If
          name = name &amp; " " &amp; seaman.LastName
  
          Return name
      End Function
  </code></pre>
  
  <p>I tried to change it to <code>If (seaman Is Nothing) Then Return DBNull</code> but I get error:</p>
  
  <blockquote>
    <p>dbnull is a type and cannot be used as an expression</p>
  </blockquote>
  
  <p>I don't really know how to fix it. Can anyone help me? </p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>[InvalidCastException: Conversion from type 'DBNull' to type 'String'
    is not valid.]<br>
    Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object
    Value) +715847<br>
    BUMSSeamenWebb.ItemsService.SeamenRow.get_FirstName() in C:\BUMS
    LOKAL\Dev\Projects\BUMSSeamenWebb\Web
    References\ItemsService\Reference.vb:51036</p>
  </blockquote>
  
  <p>That line is this code:</p>
  
  <pre><code> &lt;Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
               Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")&gt;  _
              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
  </code></pre>
  
  <p>In particular:</p>
  
  <pre><code> Return CType(Me(Me.tableSeamen.FirstNameColumn),String)
  </code></pre>
  
  <p>I can't see the problem here</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>This is another function that checks other columns</p>
  
  <pre><code> 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
  </code></pre>