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
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
Dan perhatikan Coloumn Type nya : lihat gambar
BERIKUTNYA :
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 vkdbrg, vnabar, vsat 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
*****************
Setelah itu Coding Simpan nya :
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) VALUES (@mkdbrg,@mnabar,@msatuan,@mjumlah,@mharga,@mtot)", 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
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.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
Catch ex As Exception
Beep()
MsgBox(ex.Message, MsgBoxStyle.Information, "Informasi")
cmd.Dispose()
End Try
End Sub
End Class
*************
Setelah itu Save all
Kita Cek sebelum di jalankan DataBase nya Kosong isinya :
Kemudian Jalankan / Run Programnya :
Setelah di Input Data nya kemudian tekan Tombol Simpan :
Kita Cek Tabel nya sudah ter Rekam / tidak Data tersebut :
Selamat Ber Otak Atik
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
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
Dan perhatikan Coloumn Type nya : lihat gambar
BERIKUTNYA :
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 vkdbrg, vnabar, vsat 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
*****************
Setelah itu Coding Simpan nya :
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) VALUES (@mkdbrg,@mnabar,@msatuan,@mjumlah,@mharga,@mtot)", 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
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.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
Catch ex As Exception
Beep()
MsgBox(ex.Message, MsgBoxStyle.Information, "Informasi")
cmd.Dispose()
End Try
End Sub
End Class
*************
Setelah itu Save all
Kita Cek sebelum di jalankan DataBase nya Kosong isinya :
Kemudian Jalankan / Run Programnya :
Setelah di Input Data nya kemudian tekan Tombol Simpan :
Kita Cek Tabel nya sudah ter Rekam / tidak Data tersebut :
Selamat Ber Otak Atik
Tidak ada komentar:
Posting Komentar