This code demonstrates how to insert values into a database table from a Windows form application. It opens a connection to an MS Access database, fills a dataset with data from a table, adds a new row to the dataset with values from textboxes, and updates the database table with the new row on a button click. The OleDbDataAdapter and OleDbCommandBuilder are used to sync changes between the dataset and database table.
This code demonstrates how to insert values into a database table from a Windows form application. It opens a connection to an MS Access database, fills a dataset with data from a table, adds a new row to the dataset with values from textboxes, and updates the database table with the new row on a button click. The OleDbDataAdapter and OleDbCommandBuilder are used to sync changes between the dataset and database table.
This code demonstrates how to insert values into a database table from a Windows form application. It opens a connection to an MS Access database, fills a dataset with data from a table, adds a new row to the dataset with values from textboxes, and updates the database table with the new row on a button click. The OleDbDataAdapter and OleDbCommandBuilder are used to sync changes between the dataset and database table.
Dim con As OleDbConnection Dim adt As OleDbDataAdapter Dim cmd As OleDbCommandBuilder Dim ds As New DataSet() Dim s As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\LAB - 2 PC - 20\Desktop\mmp12.mdb") con.Open() adt = New OleDbDataAdapter("select * from table1", con) adt.SelectCommand.CommandText = "select * from table1" cmd = New OleDbCommandBuilder(adt) adt.Fill(ds, "table1")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click Dim newrow As DataRow = ds.Tables("table1").NewRow newrow("ID") = TextBox1.Text newrow("name") = TextBox2.Text newrow("branch") = TextBox3.Text ds.Tables("table1").Rows.Add(newrow) adt.Update(ds, "table1") MessageBox.Show("record added successfully") End Sub End Class
(Steps :
first ms access create database
Add databse in application Use connection string properly Here used two commands so instead of command use command builder)