无法将类型 ' System.dbnull' 的对象强制转换为类型 'system.byte[]'



请帮助。无法检索图像.. im使用ODBC连接..

    sSql = "select * from Faculty where RFID='" & txtrfid.Text & "'"
            Dim cmd As New OdbcCommand(sSql, con)
            Dim dr As OdbcDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                txtfname.Text = dr("fname").ToString()
                txtlname.Text = dr("lname").ToString()
                txtid.Text = dr("STID").ToString()
                txtposition.Text = dr("Pstion").ToString()
                txtsubject.Text = dr("Subject").ToString()
                Dim bits As Byte() = CType(dr("Pfile"), Byte())'" ERROR HERE!!!""
                Dim memo As New MemoryStream(bits)
                Dim myimg As New Bitmap(memo)
                imgRetrieve.Image = myimg
                dr.Close()

在这种情况下,dr("Pfile")表达式正在返回一个DBNull值,表明列中没有任何内容。该类型和Byte()之间没有已知的转换,因此您必须手动进行

Dim data = dr("Pfile")
Dim bits as Byte()
If (TypeOf data is DBNull) Then
  bits = new Byte() { } 
Else
  bits = CType(data, Byte())
End If

最新更新