Input Data Grid di simpan di DataBase MYSQL
Kita siapkan Tabelnya Misal : temporary_beli
Buat New Project di VB-2010
Siapkan ToolBox nya :
1. Button namenya = Button2 dan Text nya = SIMPAN
2. Data Grid View = namenya dgv
3. OpenFileDialog = name ofdfoto
di dgv cari di Properties ; Coloumn - ( Collection ) Click
Ini untuk membuat Coloumn - coloumn Grid nya
Tentukan nama (name grid coloumn) dan Text nya ( text header nya )
Beri nama Coloumn Gridnya
Misal :
KODE BARANG = mkdbrg
NAMA BARANG = mnabar
SATUAN = msatuan
JUMLAH = mjumlah
HARGA = mharga
SUBTOTAL = mtot
FOTO = mgbr
Ambil Foto = btnfoto
LOKASI = mlokasi
Dan perhatikan Coloumn Type nya : lihat gambar
Kita siapkan variabel dan Koneksinya ke MYSQL
yaitu Add Module dan Add Referent nya :
ini codingnya :
Imports MySql.Data.MySqlClient
Imports System.Data
Module Module1
Public vgbr() As Byte
Public path As String = ""
Public vkdbrg, vnabar, vsat, vlokasi As String
Public vjumlah As Integer
Public vharga As Integer
Public vtot As Integer
Public cmd As New MySqlCommand
Dim serv As String = "Server=localhost;"
Dim dbase As String = "Database=toko;"
Dim uid As String = "uid=root;"
Dim pwd As String = "pwd=;"
Dim ConString = serv & dbase & uid & pwd
Public db As New MySqlConnection(ConString)
Public Sub konek()
Try
tutupDB()
db.Open()
MsgBox("Koneksi DataBase berhasil dilakukan")
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
End Try
End Sub
Public Sub tutupDB()
If db.State = ConnectionState.Open Then
db.Close()
End If
End Sub
End Module
Selanjutnya Coding untuk Aplikasinya Tombol Simpan dll
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class Form1
Private Sub dgv_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEndEdit
If e.ColumnIndex = 3 Then
dgv.Rows(e.RowIndex).Cells(5).Value = dgv.Rows(e.RowIndex).Cells(3).Value * dgv.Rows(e.RowIndex).Cells(4).Value
ElseIf e.ColumnIndex = 4 Then
dgv.Rows(e.RowIndex).Cells(5).Value = dgv.Rows(e.RowIndex).Cells(3).Value * dgv.Rows(e.RowIndex).Cells(4).Value
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
konek()
Dim cmd As New MySqlCommand("INSERT INTO temporary_beli(kdbrg,nabar,satuan,jumlah,harga,subtotal,foto,lokasi) VALUES (@mkdbrg,@mnabar,@msatuan,@mjumlah,@mharga,@mtot,@mgbr,@mlokasi)", db)
Try
For i As Integer = 0 To (dgv.Rows.Count - 2)
vkdbrg = dgv.Rows(i).Cells(0).Value
vnabar = dgv.Rows(i).Cells(1).Value
vsat = dgv.Rows(i).Cells(2).Value
vjumlah = dgv.Rows(i).Cells(3).Value
vharga = dgv.Rows(i).Cells(4).Value
vtot = dgv.Rows(i).Cells(5).Value
vlokasi = dgv.Rows(i).Cells(8).Value
' vgbr =
cmd.Parameters.AddWithValue("@mkdbrg", vkdbrg)
cmd.Parameters.AddWithValue("@mnabar", vnabar)
cmd.Parameters.AddWithValue("@msatuan", vsat)
cmd.Parameters.AddWithValue("@mjumlah", vjumlah)
cmd.Parameters.AddWithValue("@mharga", vharga)
cmd.Parameters.AddWithValue("@mtot", vtot)
cmd.Parameters.AddWithValue("@mgbr", IO.File.ReadAllBytes(vlokasi))
cmd.Parameters.AddWithValue("@mlokasi", vlokasi)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
Catch ex As Exception
Beep()
MsgBox(ex.Message, MsgBoxStyle.Information, "Informasi")
cmd.Dispose()
End Try
End Sub
Private Sub dgv_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellContentClick
Try
With ofdfoto
.Title = "Browse Gambar"
.FileName = "Pilih Gambar"
.Filter = "Type(*.jpg, *.jpeg, *.bmp, *.gif, *.png)|*.jpg; *.jpeg; *.bmp; *gif; *.png"
.ShowDialog()
path = .FileName
picphoto.Image = Image.FromFile(path)
mgbr.Image = Image.FromFile(path)
picphoto.SizeMode = PictureBoxSizeMode.StretchImage
dgv.Rows(e.RowIndex).Cells(8).Value = path
dgv.Rows(e.RowIndex).Cells(6).Value = Image.FromFile(path)
'biar tampilan image pas dengan kotak image yang di sediakan
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
End Class
Setelah itu SAVE ALL
Kita Cek sebelum di jalankan Tabel nya Kosong isinya :
Kemudian Jalankan / Run Programnya :
Kita siapkan Tabelnya Misal : temporary_beli
Buat New Project di VB-2010
Siapkan ToolBox nya :
1. Button namenya = Button2 dan Text nya = SIMPAN
2. Data Grid View = namenya dgv
3. OpenFileDialog = name ofdfoto
di dgv cari di Properties ; Coloumn - ( Collection ) Click
Ini untuk membuat Coloumn - coloumn Grid nya
Tentukan nama (name grid coloumn) dan Text nya ( text header nya )
Beri nama Coloumn Gridnya
Misal :
KODE BARANG = mkdbrg
NAMA BARANG = mnabar
SATUAN = msatuan
JUMLAH = mjumlah
HARGA = mharga
SUBTOTAL = mtot
FOTO = mgbr
Ambil Foto = btnfoto
LOKASI = mlokasi
Dan perhatikan Coloumn Type nya : lihat gambar
Kita siapkan variabel dan Koneksinya ke MYSQL
yaitu Add Module dan Add Referent nya :
ini codingnya :
Imports MySql.Data.MySqlClient
Imports System.Data
Module Module1
Public vgbr() As Byte
Public path As String = ""
Public vkdbrg, vnabar, vsat, vlokasi As String
Public vjumlah As Integer
Public vharga As Integer
Public vtot As Integer
Public cmd As New MySqlCommand
Dim serv As String = "Server=localhost;"
Dim dbase As String = "Database=toko;"
Dim uid As String = "uid=root;"
Dim pwd As String = "pwd=;"
Dim ConString = serv & dbase & uid & pwd
Public db As New MySqlConnection(ConString)
Public Sub konek()
Try
tutupDB()
db.Open()
MsgBox("Koneksi DataBase berhasil dilakukan")
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
End Try
End Sub
Public Sub tutupDB()
If db.State = ConnectionState.Open Then
db.Close()
End If
End Sub
End Module
Selanjutnya Coding untuk Aplikasinya Tombol Simpan dll
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class Form1
Private Sub dgv_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEndEdit
If e.ColumnIndex = 3 Then
dgv.Rows(e.RowIndex).Cells(5).Value = dgv.Rows(e.RowIndex).Cells(3).Value * dgv.Rows(e.RowIndex).Cells(4).Value
ElseIf e.ColumnIndex = 4 Then
dgv.Rows(e.RowIndex).Cells(5).Value = dgv.Rows(e.RowIndex).Cells(3).Value * dgv.Rows(e.RowIndex).Cells(4).Value
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
konek()
Dim cmd As New MySqlCommand("INSERT INTO temporary_beli(kdbrg,nabar,satuan,jumlah,harga,subtotal,foto,lokasi) VALUES (@mkdbrg,@mnabar,@msatuan,@mjumlah,@mharga,@mtot,@mgbr,@mlokasi)", db)
Try
For i As Integer = 0 To (dgv.Rows.Count - 2)
vkdbrg = dgv.Rows(i).Cells(0).Value
vnabar = dgv.Rows(i).Cells(1).Value
vsat = dgv.Rows(i).Cells(2).Value
vjumlah = dgv.Rows(i).Cells(3).Value
vharga = dgv.Rows(i).Cells(4).Value
vtot = dgv.Rows(i).Cells(5).Value
vlokasi = dgv.Rows(i).Cells(8).Value
' vgbr =
cmd.Parameters.AddWithValue("@mkdbrg", vkdbrg)
cmd.Parameters.AddWithValue("@mnabar", vnabar)
cmd.Parameters.AddWithValue("@msatuan", vsat)
cmd.Parameters.AddWithValue("@mjumlah", vjumlah)
cmd.Parameters.AddWithValue("@mharga", vharga)
cmd.Parameters.AddWithValue("@mtot", vtot)
cmd.Parameters.AddWithValue("@mgbr", IO.File.ReadAllBytes(vlokasi))
cmd.Parameters.AddWithValue("@mlokasi", vlokasi)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
Catch ex As Exception
Beep()
MsgBox(ex.Message, MsgBoxStyle.Information, "Informasi")
cmd.Dispose()
End Try
End Sub
Private Sub dgv_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellContentClick
Try
With ofdfoto
.Title = "Browse Gambar"
.FileName = "Pilih Gambar"
.Filter = "Type(*.jpg, *.jpeg, *.bmp, *.gif, *.png)|*.jpg; *.jpeg; *.bmp; *gif; *.png"
.ShowDialog()
path = .FileName
picphoto.Image = Image.FromFile(path)
mgbr.Image = Image.FromFile(path)
picphoto.SizeMode = PictureBoxSizeMode.StretchImage
dgv.Rows(e.RowIndex).Cells(8).Value = path
dgv.Rows(e.RowIndex).Cells(6).Value = Image.FromFile(path)
'biar tampilan image pas dengan kotak image yang di sediakan
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
End Class
Setelah itu SAVE ALL
Kita Cek sebelum di jalankan Tabel nya Kosong isinya :
Kemudian Jalankan / Run Programnya :
Selamat Ber Otak Atik
Tidak ada komentar:
Posting Komentar