Koneksi antara datagridview dengan database acces vb 2013

Jun

28

2016

Mau tanya nih kak senior. kan di vb udah bikin datagrid dan udah koneksi ke database. ketika di run pertama, misal barang berkode BRG001 dihapus, di datagrid ikut kehapus. tapi stelah dirun ke dua, data didatagrid yg tadi dihapus muncul lagi,begitu juga data didatabase gmana yh?

dan ini koding yang saya gunakan

Imports System.Data.OleDb

Module Module1

Public conn As OleDbConnection = New OleDbConnection
Public RD As OleDbDataReader

Public DA As OleDbDataAdapter

Public DS As DataSet

End Module

Public Class Form1
Dim connstring As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
‘TODO: This line of code loads data into the ‘Database1DataSet1.Mumet’ table. You can move, or remove it, as needed.
‘Me.MumetTableAdapter1.Fill(Me.Database1DataSet1.Mumet)
‘TODO: This line of code loads data into the ‘Mumet’ table. You can move, or remove it, as needed.
‘Me.MumetTableAdapter.Fill(Me.Database1DataSet.Mumet)
TampilData()

End Sub
Sub koneksi()

connstring = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.mdb”
conn.ConnectionString = connstring
conn.Open()
End Sub
Sub Tambah()

Call koneksi()

Dim str As String
str = “Insert Into Mumet([KodeBarang],[NamaBarang],[HargaBeli],[HargaJual],[Jumlah]) Values (?,?,?,?,?)”
Dim cmd As OleDbCommand = New OleDbCommand(str, conn)
cmd.Parameters.Add(New OleDbParameter(“KodeBarang”, CType(Txtkode.Text, String)))
cmd.Parameters.Add(New OleDbParameter(“NamaBarang”, CType(Txtnama.Text, String)))
cmd.Parameters.Add(New OleDbParameter(“HargaBeli”, CType(Txtbeli.Text, String)))
cmd.Parameters.Add(New OleDbParameter(“HargaJual”, CType(Txtjual.Text, String)))
cmd.Parameters.Add(New OleDbParameter(“Jumlah”, CType(Txtjumlah.Text, String)))

Try
cmd.ExecuteNonQuery()
MessageBox.Show(“Simpan Data Berhasil Dilakukan”)
cmd.Dispose()
conn.Close()
Txtkode.Clear()
Txtnama.Clear()
Txtbeli.Clear()
Txtjual.Clear()
Txtjumlah.Clear()

Catch ex As Exception
MsgBox(ex.Message)

End Try

End Sub
Private Sub TampilData()
Using conn As New OleDbConnection(My.Settings.Database1ConnectionString)
Using DA As New OleDbDataAdapter(“Select*From Mumet”, conn)
conn.Open()
Dim Database1 As New DataTable
DA.Fill(Database1)
DataGridView1.DataSource = Database1

End Using
End Using
End Sub

Private Sub Cmdtambah_Click(sender As Object, e As EventArgs) Handles Cmdtambah.Click

Call Tambah()

Call TampilData()
End Sub

Private Sub Cmdubah_Click(sender As Object, e As EventArgs) Handles Cmdubah.Click
Dim Edit As String = “Update Mumet Set NamaBarang='” & Txtnama.Text & “‘,HargaBeli='” & Txtbeli.Text & “‘,HargaJual='” & Txtjual.Text & “‘,Jumlah='” & Txtjumlah.Text & “‘Where KodeBarang='” & Txtkode.Text & “‘”
Try
Using Conn As New OleDbConnection(My.Settings.Database1ConnectionString)
Using cmd As New OleDbCommand(Edit, Conn)
Conn.Open()
cmd.ExecuteNonQuery()
MsgBox(“Data Berhasil Diubah”, MsgBoxStyle.Information, “Perhatian”)
Call TampilData()

End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Cmdkeluar.Click
End ‘untuk keluar dari program
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Cmdhapus.Click
Dim Hapus As String = “Delete From Mumet Where KodeBarang='” & Txtkode.Text & “‘”
Try
Using Conn As New OleDbConnection(My.Settings.Database1ConnectionString)
Using cmd As New OleDbCommand(Hapus, Conn)
Conn.Open()
cmd.ExecuteNonQuery()
MsgBox(“Data Berhasil Dihapus”, MsgBoxStyle.Information, “Perhatian”)

Call TampilData()

End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

End Class

Kategori: .Net (VB and C#) Ditanyakan oleh: [2 Grey Star Level]

Jawab Pertanyaan

Anda harus Log In untuk menjawab pertanyaan.

Belum bergabung? Daftar Sekarang »