Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please help me.... how save image from picturebox, and how load image in event datagridview clik to picturebox,i use mysql database, this is my coding.. :

note : pb=picturebox, dg=datagridview, baranggambar longblob= my name fild image

'this is my coding for save :

Sub proses(ByVal sql As String)
Dim FileSize As UInt32

Dim mstream As New System.IO.MemoryStream()
pb.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
FileSize = mstream.Length
mstream.Close()
MsgBox(FileSize)
kon.Close()
kon.Open()
perintah.Connection = kon
perintah.CommandType = CommandType.Text
perintah.CommandText = sql
perintah.Parameters.AddWithValue("photo", arrImage)
perintah.ExecuteNonQuery()
MsgBox("Data berhasil di proses!!!", MsgBoxStyle.Information, "info")
kon.Close()
End Sub

Private Sub Bsimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bsimpan.Click
If (Txtidbrg.Text = "") Or (Txtnamabrg.Text = "") Or (Txtharga.Text = "") Or (Txtmodal.Text = "") Or (Txtkategori.Text = "") Or (Txtstok.Text = "") Then
MsgBox("lengkapi dulu data bertanda *...", MsgBoxStyle.Information, "info")
Exit Sub
End If

If Bsimpan.Text = "Simpan" Then
Bsimpan.Text = "update"
kon.Open()
perintah.Connection = kon
perintah.CommandType = CommandType.Text
perintah.CommandText = "select * from barang where barangid='" & Txtidbrg.Text & "'"
cek = perintah.ExecuteReader
cek.Read()
If cek.HasRows Then
If cek.Item("barangid") <> 0 Then
MsgBox("kode sudah ada harap diganti", MsgBoxStyle.Information)
End If
Else

Call proses("insert into barang(barangid,barangnama,barangkategori,barangidgambar,baranggambar,barangmodal,barangharga,barangstok)values('" & Txtidbrg.Text & "','" & Txtnamabrg.Text & "','" & Txtkategori.Text & "','" & Txtgambar.Text & "','" & "photo" & "' ,'" & Txtmodal.Text & "','" & Txtharga.Text & "','" & Txtstok.Text & "')")

cek.Close()
kon.Close()

End If

'this my coding for load :
Private Sub dg_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellContentClick
Dim i As Integer
i = dg.CurrentRow.Index
If dg.CurrentCell.Value.ToString = "" Then
MsgBox("data tidak ada gan...")

Exit Sub
Else
kon.Open()
perintah.Connection = kon
perintah.CommandType = CommandType.Text
perintah.CommandText = "SELECT * FROM barang where barangid='" & dg.CurrentRow.Cells(0).Value.ToString & "'"
cek = perintah.ExecuteReader
cek.Read()
If cek.HasRows Then

pb.Image = pbGetDataToImage(cek.Item("baranggambar"))

End If
cek.Close()
kon.Close()

Txtidbrg.Text = dg.Rows.Item(i).Cells(0).Value
Txtnamabrg.Text = dg.Rows.Item(i).Cells(1).Value
Txtkategori.Text = dg.Rows.Item(i).Cells(2).Value
Txtgambar.Text = dg.Rows.Item(i).Cells(3).Value
Txtstok.Text = dg.Rows.Item(i).Cells(4).Value
Txtmodal.Text = dg.Rows.Item(i).Cells(5).Value
Txtharga.Text = dg.Rows.Item(i).Cells(6).Value


End If
Posted
Comments
Sergey Alexandrovich Kryukov 1-Dec-14 10:11am    
And what is the problem? Could you properly format the code sample using "Improve question" (use "pre" tags)?
—SA
dedet masri yuni 1-Dec-14 23:36pm    
the problom i cant show display image to picturebox, this mesage "parameter is not valid"

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