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

时间:2014-02-05 17:56:26

标签: vb.net visual-studio-2010

请帮忙。无法检索图像..即时通讯使用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()

1 个答案:

答案 0 :(得分:2)

在这种情况下,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