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
If Not IsDBNullthen perform your checks...Do keep in mind thatDBNullis NOT in fact the same asNothingDataRowtype have extension method.Field(Of T), which can be used forStringwithoutnullchecking. ReturnNothingif value isDBNull