Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys I have a form which is to Add, Edit. In below code I save an image to database which works correctly. I want when the form is in Edit function to see the image in the picturebox.


VB
Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles ButtonSave.Click
        Dim ms As New IO.MemoryStream()
        Dim arrimage() As Byte = ms.GetBuffer
        pictBox.Image.Save(ms, pictBox.Image.RawFormat)
        If txtLname.Text = "" Or txtFtname.Text = "" Or _
            txtCity.Text = "" Then
        End If
        If frUpdate = True Then
            Dim cmd As New SqlCommand()
            cmd.CommandText = "UPDATE Staff SET Lastname = @Lastname, Firstname = @Firstname, City = @City, Photo = @Photo WHERE StaffID = @StaffID"
            With cmd.Parameters
                .Add(New SqlParameter("@Lastname", txtLname.Text))
                .Add(New SqlParameter("@Firstname", txtFname.Text))
                .Add(New SqlParameter("@City", txtCity.Text))
                .Add(New SqlParameter("@Photo", SqlDbType.Image)).Value = arrimage
                .Add(New SqlParameter("@StaffID", txtAA.Text))
            End With
            cmd.Connection = clsMSSQL.con
            cmd.ExecuteNonQuery()
            MsgBox("Επιτυχημένη Ενημέρωση!", MsgBoxStyle.Information, "Πληροφορία")
            frmList.cmdRefresh.PerformClick()
            Close()
            Exit Sub
        Else
            Dim cmd As New SqlCommand()
            cmd.CommandText = "INSERT INTO Staff(Lastname,Firstname,City,Photo)" & _
                                 "VALUES (@Lastname,@Firstname,@City,@Photo)"
            With cmd.Parameters
                .Add(New SqlParameter("@Lastname", txtLname.Text))
                .Add(New SqlParameter("@Firstname", txtFname.Text))
                .Add(New SqlParameter("@City", txtCity.Text))
                .Add(New SqlParameter("@Photo", SqlDbType.Image)).Value = arrimage
            End With
            cmd.Connection = clsMSSQL.con
            cmd.ExecuteNonQuery()
            MsgBox("Επιτυχημένη Αποθήκευση!", MsgBoxStyle.Information, "Πληροφορία")
            Call LoadID()
        End If
        frmList.cmdRefresh.PerformClick()
        Exit Sub
    End Sub


Thanks in advance

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 13-Feb-13 1:14am
v3

Have a look at this CodeProject article Save and Retrieve Image from a SQL Server Database using VB.NET[^]
 
Share this answer
 
Comments
jomachi 12-Feb-13 11:03am    
Thanks my friend
Your insert / update code looks fine - so all you have to do is read the data back from the database, convert it to a byte array and then load it as an Image from that:
VB
Dim ms As New MemoryStream(bytes)
Dim returnImage As Image = Image.FromStream(ms)
Presumably you know how to read it back - your other code looks proficient, so a SELECT command shouldn't be beyond you! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900