Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Call Koneksi()
        Dim Cmd As SqlCommand
        Dim edit As String = "Update tbl_image set [Nama Barang]= @Nama_Barang , Jumlah= @Jumlah , [Stok Barang]=  @Stok_Barang , Gambar = @Gambar where Kode=  @Kode "
        Cmd = New SqlCommand(edit, Conn)
        Cmd.Parameters.AddWithValue("@Kode", TextBox1.Text)
        Cmd.Parameters.AddWithValue("@Nama_Barang", ComboBox1.Text)
        Cmd.Parameters.AddWithValue("@Jumlah", TextBox2.Text)
        Cmd.Parameters.AddWithValue("@Stok_Barang", TextBox3.Text)
        Dim MemoryStream As New MemoryStream
        PictureBox1.Image.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Bmp)
        Dim Dgambar As Byte() = MemoryStream.GetBuffer
        Dim Images As New SqlParameter("@Gambar", Dgambar)
        Images.Value = Dgambar
        Cmd.Parameters.Add(Images)
        Cmd.ExecuteNonQuery()
        MsgBox("Data Berhasil Di Update", MsgBoxStyle.Information, "Information")
        Call KondisiAwal()


What I have tried:

I entered this code to insert an image, but if I can't update it

Dim MemoryStream As New MemoryStream
        PictureBox1.Image.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Bmp)
        Dim Dgambar As Byte() = MemoryStream.GetBuffer
        Dim Images As New SqlParameter("@Gambar", Dgambar)
        Images.Value = Dgambar
        Cmd.Parameters.Add(Images)
        Cmd.ExecuteNonQuery()
Posted
Updated 22-Aug-19 10:30am
Comments
Richard MacCutchan 22-Aug-19 11:28am    
What errors do you receive?
Richard Deeming 22-Aug-19 11:29am    
GetBuffer will return the underlying buffer, which may be longer than the data written to the stream. Use ToArray instead.

Other than that, you're going to need to provide a lot more details of the problem if you want anyone to be able to help you.

1 solution

Well, seems your issue is: how to convert image to byte array and vice versa...

You can use these functions:
VB
'convert image to bytearray
Public Function imgToByteArray(ByVal img As Image) As Byte()
    Using mStream As New MemoryStream()
        img.Save(mStream, img.RawFormat)
        Return mStream.ToArray()
    End Using
End Function
'convert bytearray to image
Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
    Using mStream As New MemoryStream(byteArrayIn)
        Return Image.FromStream(mStream)
    End Using
End Function

More at: Convert Image to Byte Array and Byte Array to Image c# , VB.Net[^]

Usage:
VB
Dim Dgambar As Byte() = imgToByteArray(PictureBox1.Image)
Dim img As New SqlParameter("@Gambar", Dgambar)
img.Value = Dgambar
Cmd.Parameters.Add(img)
Cmd.ExecuteNonQuery()
 
Share this answer
 
Comments
Smart Friend 23-Aug-19 9:23am    
still can't when updated

How to convert image to string ?
Maciej Los 23-Aug-19 9:35am    
You can't convert image to string. You need to convert it into byte array, what i already have shown you.
Smart Friend 23-Aug-19 23:07pm    
the problem is solved and can be updated
'convert image to bytearray
Public Function imgToByteArray(ByVal img As Image) As Byte()
Using mStream As New MemoryStream()
img.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Return mStream.ToArray()
End Using
End Function
Thank you, sir
Maciej Los 24-Aug-19 1:21am    
You're very welcome.

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