Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

Update Button for Access Database via DataGridView Using OLEDB in VB.NET (Visual Studio 2013)

I have linked an Access database to my program. It populates the DataGridView as it is intended to so that part of the program works. However the new data that i add to my DataGriedView wont show up and i didnt know what is wrong with my code

Can anyone see anything wrong or something I've missed out that would cause the code not to function as desired? Thank you in advance :)

Imports System.Data.OleDb
Public Class Form1
    Dim j As OleDbConnection
    Dim a As OleDbDataAdapter
    Dim s As DataSet
    Dim lokasidb As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call jaringan()
        a = New OleDbDataAdapter("Select * From datadairy", j)
        s = New DataSet
        s.Clear()
        a.Fill(s, "datadairy")
        DataGridDairy.DataSource = (s.Tables("datadairy"))
    End Sub
    Private Sub eksekusiSql(ByVal Sql As String)
        Dim objcmd As New System.Data.OleDb.OleDbCommand
        Call jaringan()
        Try
            objcmd.Connection = j
            objcmd.CommandType = CommandType.Text
            objcmd.CommandText = Sql
            objcmd.ExecuteNonQuery()
            objcmd.Dispose()
            MsgBox("The new data successfully saved", vbInformation)
        Catch ex As Exception
            MsgBox("The new data is failed to save", vbInformation)
        End Try
    End Sub
    Sub jaringan()
        lokasidb = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|19106060045_Tugas Modul 5.accdb"
        j = New OleDbConnection(lokasidb)
        If j.State = ConnectionState.Closed Then j.Open()
    End Sub
    Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
        Dim No As String = TextNo.Text
        Dim Jenis_Susu_Sapi As String = TextSusu.Text
        Dim Jenis_Olahan As String = TextOlahan.Text
        Dim Harga_per_kg As String = TextHarga.Text
        Dim Tempat_Penjualan As String = TextPasar.Text
        Dim Sql_Simpan_Dairy As String = "Insert into datadairy (No, Jenis_Susu_Sapi, Jenis_Olahan, Harga_per_kg, Tempat_Penjualan) values (" + No + ",'" + Jenis_Susu_Sapi + "','" + Jenis_Olahan + "','" + Harga_per_kg + "','" + Tempat_Penjualan + "')"

        eksekusiSql(Sql_Simpan_Dairy)
        ShowDairydata()

    End Sub
    Public Sub ShowDairydata()
        Call jaringan()
        a = New OleDbDataAdapter("Select * from datadairy", j)
        s = New DataSet
        s.Clear()
        a.Fill(s, "datadairy")
        DataGridDairy.DataSource = (s.Tables("datadairy"))
    End Sub
question from:https://stackoverflow.com/questions/65713592/update-button-for-access-database-via-datagridview-using-oledb-in-vb-net-visual

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After adding new data to your database, just use

'DataGridDairy.Databind()'

to refresh.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...