0

help please. cannot retrieve the image.. im using Odbc connection..

    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()
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
user3275820
  • 1
  • 1
  • 2
  • possible duplicate of [Unable to cast object of type 'System.DBNull' to type 'System.String\`](http://stackoverflow.com/questions/870697/unable-to-cast-object-of-type-system-dbnull-to-type-system-string) – usr Jun 22 '14 at 09:54

1 Answers1

2

In this case the dr("Pfile") expression is returning a DBNull value indicating that nothing is in the column. There is no known conversion between that type and a Byte() hence you will have to do it manually

Dim data = dr("Pfile")
Dim bits as Byte()
If (TypeOf data is DBNull) Then
  bits = new Byte() { } 
Else
  bits = CType(data, Byte())
End If
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • TypeOf data is DBNull ERROR. value of type 'String' cannot be converted tp '1- dimensional array of type ' im using vb.net 2008 . – user3275820 Feb 05 '14 at 18:06
  • 1
    @user3275820 so it sounds like in the non DBNull case you don't actually have a `byte()` value but a `String` Is that the case? – JaredPar Feb 05 '14 at 18:21
  • I think so... What should I do to retrieve the image data? Thank you – user3275820 Feb 05 '14 at 18:39
  • @user3275820 you would need to convert directly to a `String` then use an enocding to get the `Byte()` for that value – JaredPar Feb 05 '14 at 19:06