簡體   English   中英

無法將類型為“ System.DBNull”的對象轉換為類型為“ System.Byte []”的對象。

[英]Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

當我嘗試在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())

我真的不知道該放在哪里。 這里只是一個新手。

由於可能以前沒有為行保存任何圖像數據,因此在嘗試使用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

使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM