2016-06-07 166 views
0

我正在从一个项目中获取数据库中的数据。错误:从'DBNull'类型到'String'类型的转换无效

现在我收到此错误信息:

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

我知道是什么错误意味着,我只是需要一种方法来解决它。

我有以下代码:

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 

我试图将其更改为If (seaman Is Nothing) Then Return DBNull,但我得到的错误:

dbnull is a type and cannot be used as an expression

我真的不知道如何解决它。谁能帮我?

UPDATE:

我得到这个错误:

[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

这行是该代码:

<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 

特别是:

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

我看不出这里的问题

更新2:

这是检查其他列

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 
+0

@sstan谢谢,我更新了我的问题。如果你能再次检查,我很感激。 –

+0

'如果不是IsDBNull',那么执行您的检查...请记住,'DBNull'实际上与'Nothing'不一样。 – user2366842

+0

另外,作为一个附注,最好检查一下,确保你输入INT的值实际上是数字,然后再尝试投它们...... – user2366842

回答

1

这些产生DataRow类以这样的方式编码的另一功能是,如果你尝试读取具有数据库空值的属性(列值),它将引发异常。

在试图读取它之前,您需要检查为空

从消息中可以看出,在这种情况下,您抱怨说您正在尝试读取FirstName的值,但它为空(DbNull)。

通常,当某列可以为空时,这些DataRow类还包含一个便利函数,用于检查该特定列上的空值。所以在这种情况下,ItemsDataSet.SeamenRow也应该有IsFirstNameNull函数。如果是这样,请先打电话,并处理该特定情况。如果IsFirstNameNull返回false,则可以安全读取FirstName属性。

如果您的数据库中有其他列可以为空,则需要遵循相似的模式。

+0

我确实有一个检查null的'IsFirstNameNull'函数,我从来没有用过它。我有另一个检查null的函数,请检查我的** Update 2 **。 IsFirstNameNull是否应该以类似的方式工作?谢谢。 –

+0

是的,确切地说。这就是模式。 – sstan

相关问题