Click here to Skip to main content
11,647,018 members (78,643 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.


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 12-Feb-13 4:42am
jomachi1.6K
Edited 13-Feb-13 1:14am
joe_j4.1K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
jomachi at 12-Feb-13 11:03am
   
Thanks my friend
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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 | :laugh:
  Permalink  

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

  Print Answers RSS
0 jyo.net 510
1 Sergey Alexandrovich Kryukov 433
2 OriginalGriff 388
3 CPallini 334
4 F-ES Sitecore 309
0 OriginalGriff 1,253
1 jyo.net 994
2 DamithSL 896
3 Sergey Alexandrovich Kryukov 833
4 CPallini 720


Advertise | Privacy | Mobile
Web03 | 2.8.150804.2 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100