无法将类型为“System.DBNull”的对象强制转换为“System.Byte []”。

时间:2014-10-14 19:55:27

标签: mysql vb.net

当我尝试在ListView中选择我的数据库中没有图像的项时,此错误显示Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.我试图提供一些代码,例如isDBNullDBNull但它适用

这是我的代码:

Private Sub LvPeople_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LvPeople.SelectedIndexChanged
        If LvPeople.SelectedItems.Count > 0 Then
            Dim connstring As String = "server = localhost; user id = root; database = db; password = root"
            Dim Sql As String = "select * from candidate where idn='" & LvPeople.SelectedItems(0).Text & "'"
            Dim conn As New MySqlConnection(connstring)
            Dim cmd As New MySqlCommand(Sql, conn)
            Dim dr As MySqlDataReader = Nothing
            conn.Open()
            dr = cmd.ExecuteReader()
            dr.Read()
            Dim imagebytes As Byte() = CType(dr("photo"), Byte())
            Using ms As New IO.MemoryStream(imagebytes)
                PictureBox1.Image = Image.FromStream(ms)
                PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            End Using
            conn.Close()
        End If
End Sub
End Class

错误点在这里:

Dim imagebytes As Byte() = CType(dr("photo"), Byte())

我真的不知道该把什么放在这里。这里只是个新手。

2 个答案:

答案 0 :(得分:1)

由于以前没有为行保存的图像数据,您需要在尝试使用之前测试DBNull:

If IsDBNull(dr("photo")) = False Then
    Dim imagebytes As Byte() = CType(dr("photo"), Byte())
    Using ms As New IO.MemoryStream(imagebytes)
        PictureBox1.Image = Image.FromStream(ms)
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End Using
Else
      ' maybe display a "no Photo Available" stock image
End If

请注意,此DBNull测试与Steve使用的测试不同。 IsDBNull是一个语言函数,而他正在使用的是DataReader对象的方法,这也是为什么有不同的要求。然而第三种方法是将其与System.DbNull进行比较:

If DBNull.Value.Equals(dr("photo")) = False Then
    ...
End If

答案 1 :(得分:0)

使用DataReader方法IsDBNull,但此方法需要读取器使用的IDataRecord中字段的位置,因此您还需要使用要检查的字段名称调用GetOrdinal (链接指向Sql Server版本,但它们对于MySql是相同的)

Private Sub LvPeople_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LvPeople.SelectedIndexChanged
    If LvPeople.SelectedItems.Count > 0 Then
         Dim connstring As String = "...."
         Dim Sql As String = "select * from candidate where idn=@id"
         Using conn = new MySqlConnection(connstring)
         Using cmd = new MySqlCommand(Sql, conn)
            conn.Open()
            cmd.Parameters.AddWithValue("@id", LvPeople.SelectedItems(0).Text)
            Using dr = cmd.ExecuteReader()
               if dr.Read() Then
                  if Not dr.IsDbNull(dr.GetOrdinal("photo")) Then
                     Dim imagebytes As Byte() = CType(dr("photo"), Byte())
                     Using ms As New IO.MemoryStream(imagebytes)
                         PictureBox1.Image = Image.FromStream(ms)
                         PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
                     End Using
                 End If
              End If
           End Using
        End Using
        End Using

    End If
End Sub