H! I'm trying to upload an image in a DB2 database. The image size is a JPG (6.76 kb - 6924 bytes).
The database table has a BLOB field of length 1048576.
My code to insert the image is as follows:
If fileup.PostedFile IsNot Nothing AndAlso fileup.PostedFile.FileName <> "" Then
Dim imagesize As Byte() = New Byte(fileup.PostedFile.ContentLength - 1) {}
Dim uploadedimage1 As HttpPostedFile = fileup.PostedFile
uploadedimage1.InputStream.Read(imagesize, 0, CInt(fileup.PostedFile.ContentLength))
Dim uploadedimage2 As New OleDbParameter("@Image", OleDbType.VarBinary, imagesize.Length)
uploadedimage2.Value = imagesize
Dim cmd As New OleDbCommand()
cmd.CommandText = "INSERT INTO xxx_TBL(x, IMAGE) VALUES (?, ?)"
cmd.Parameters.Add(x)
cmd.Parameters.Add(uploadedimage2)
cmd.Connection = clsDatabase.Open_DB()
Dim result As Integer = cmd.ExecuteNonQuery()
If result > 0 Then
Return True
The image gets inserted into the Database.
The code to get the image from the Database, into a DataTable, which is then bound to a GridView to display on the webpage is as follows:
Dim cmd As New OleDbCommand()
cmd.CommandText = "SELECT x, IMAGE FROM xxx_TBL WHERE y = 1"
cmd.Connection = clsDatabase.Open_DB()
Dim dReader As OleDbDataReader
dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Comments", GetType(String)))
dt.Columns.Add(New DataColumn("Picture", GetType(Bitmap)))
Do While (dReader.Read())
Dim dr As DataRow = dt.NewRow()
dr("Comments") = dReader(0).ToString
Dim imageobj = dReader(1)
Using ms As New System.IO.MemoryStream
Dim bm As Bitmap
Dim bytearray = DirectCast(imageobj, Byte())
ms.Write(bytearray, 0, bytearray.length)
bm = New Bitmap(ms)
dr("Picture") = bm
End Using
dt.Rows.Add(dr)
Loop
GridView1.DataSource = dt
GridView1.DataBind()
x comes from the Database fine, and is displayed. However, I get no picture - just a small "missing picture (red cross on white) icon".
On checking the database, the length of the image in BLOB field is 8192. However, on copying it onto a 'TEST' File (no extension), the size was 13848 bytes. I'm guessing that could be because of the way DB2 reads/encodes the image binary, but I'm not sure. Could someone please highlight the possible causes of error? Any suggestions on making this work or debugging?