Hello,

I have been searching everywhere to find out how I can save a picture from a PictureBox into SQL. Someone told me the best way to do it, is to convert it to a binary file and save it like that.

This is what I have, it probably makes no sense

SavePicture Picture1.Picture, "C:\MyPict.bmp"

strSQL = "Select * from tblError"
RS.CursorLocation = adUseClient
RS.Open strSQL, CONN, adOpenStatic, adLockOptimistic
RS.AddNew

Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile "C:\MyPict.bmp"
RS.Fields("Picture").value = strStream.Read ---> this is where I get an error

Can someone plaese advise, I am running out of advil....

Never mind..for those who are having problems this is what I did

'''''''''''''''''''''' Saving the file
Dim strSQL As String
Dim rs As ADODB.Recordset
Dim SavePictureToDB As Boolean
Dim strStream As ADODB.Stream, sFileName As String
Set rs = New ADODB.Recordset

SavePicture Image1.Picture, "C:\MyPict.bmp"

strSQL = "Select * from tblError"
rs.CursorLocation = adUseClient
rs.Open strSQL, CONN, adOpenStatic, adLockOptimistic
rs.AddNew

Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile "C:\MyPict.bmp"
strStream.Flush
rs.Fields("Picture").value = strStream.Read
'Rest of the fields
rs.Fields("dtmDate").value = Date
rs.Fields("strErrorDescription").value = txtError
rs.Update
rs.Close
strStream.Close


''''''''''''''''''''''' Getting the file back
On Error GoTo procNoPicture
strSQL = "Select picture from tblError where cntErrorID = " & CLng(TLError.Grid.Cells(TLError.listIndex + 1, 4).value)
rs.CursorLocation = adUseClient
rs.Open strSQL, CONN
'If Recordset is Empty, Then Exit
If rs.recordCount = 0 Then
GoTo procNoPicture
End If

Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.Write (rs!Picture)

strStream.SaveToFile "C:\Temp.bmp", adSaveCreateOverWrite
'
Image1 = LoadPicture("C:\Temp.bmp")
Kill ("C:\Temp.bmp")


Also, make sure that your SQL field is set as Image...

Hope this helps...........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.