I am inserting and retrieving an image from my database. I can now insert but I am having a hard time retrieving the file. I used varbinary(max) as datatype of the image.
This is my code for inserting:
Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim img() As Byte
img = ms.ToArray()
cmd.CommandText = "insert into stud values ('" & studno.Text & "', '" & password.Text & "', '" & fname.Text & "', '" & mname.Text & "', '" & lname.Text & "', @img, '" & gender.Text & "', '" & mm.Text & "/" & dd.Text & "/" & yyyy.Text & "', '" & phone.Text & "', '" & address.Text & "', 'Student', '" & secquest.Text & "', '" & answersq.Text & "', '" & TextBox1.Text & "', '" & ComboBox1.Text & "')"
cmd.Parameters.Add("@img", SqlDbType.VarBinary).Value = img
and this is how i retrieve:
con.Open()
cmd.CommandText = "select * from stud where studentno = 'mnb'"
cmd.Connection = con
dr = cmd.ExecuteReader()
While dr.Read()
studnum.Text = dr.Item("studentno")
fname.Text = dr.Item("fname")
mname.Text = dr.Item("mname")
lname.Text = dr.Item("lname")
gender.Text = dr.Item("gender")
section.Text = dr.Item("seccode")
bday.Text = dr.Item("bday")
phone.Text = dr.Item("phoneno")
address.Text = dr.Item("maddress")
Dim imageData As Byte() = DirectCast(dr("pic"), Byte())
If Not imageData Is Nothing Then
Using ms As New MemoryStream(imageData, 0, imageData.Length)
ms.Write(imageData, 0, imageData.Length)
PictureBox1.BackgroundImage = Image.FromStream(ms, True)
End Using
End If
End While
My problem is, it says OUT OF MEMORY whenever i run my program. How to solve it? The size of the image i am retrieving is 2MB.
I am using memorystream and what I researched most used filestream. I believe it's different in some ways.