i have problem when save image use MS Access Database.
field name is pict -> OLE OBJECT
hire my code when save image :
vb.net Code:
'simpan file gambar Dim img As Image = ucPhoto.getGambar If img IsNot Nothing Then Dim imgPar As New OleDbParameter Dim fs As FileStream Dim sfname As String = ucPhoto.FileName fs = New FileStream(sfname, FileMode.Open, FileAccess.Read) Dim picByte As Byte() = New Byte(CInt(fs.Length - 1)) {} fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length)) fs.Close() imgPar.OleDbType = OleDbType.Binary imgPar.ParameterName = "pict" imgPar.Value = picByte End If
when i run that code is not problem. but when i run code to display image i get error:
vb.net Code:
If dr.Item("pict") IsNot Nothing Then Dim fsImage As New IO.FileStream("img.jpg", IO.FileMode.Create) Dim blob As Byte() = DirectCast(dr.Item("pict"), Byte()) fsImage.Write(blob, 0, blob.Length) fsImage.Close() fsImage = Nothing ucPhoto.setImage(Image.FromFile("img.jpg")) End If
error at line :
error message :Code:Dim blob As Byte() = DirectCast(dr.Item("gambar"), Byte())
thank youCode:Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'![]()




Reply With Quote