Start New Discussion within our Software Development Community

how to update database with vb.net form

Here is a little tutorial.

Imports System.Data.OleDb

First setup your Database connection.
Put the next bit of code in your Form's Public class

Dim dbInsert As New OleDb.OleDbCommand
Dim dbConnect As New OleDb.OleDbConnection

Put the next bit of code in your Form Load Private Sub

Try
  dbConnect.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Database\Main.mdb"
  dbConnect.Open()
Catch ex As Exception
  MessageBox.Show(ex.Message)
  Me.Close()
End Try

Lets say you have 3 text boxes txtName, txtSurname & txtNumber and you want to update the database with the data in the boxes, put the following code in the update button click event Private Sub.

*NOTE: col_Name, col_Surname, col_Number are your column names in your database.

dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "col_Name"
dbInsert.Parameters.Item("col_Name").Value = txtName.Text
dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "col_Surname"
dbInsert.Parameters.Item("col_Surname").Value = txtSurname.Text
dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "col_Number"
dbInsert.Parameters.Item("col_Number").Value = txtNumber.Text

*NOTE: Table_Person is your table in the database.

dbInsert.CommandText = "INSERT INTO Table_Person VALUES (txtName.Text, txtSurname.Text, txtNumber.Text);"
dbInsert.CommandType = CommandType.Text
dbInsert.Connection = dbConnect
dbInsert.ExecuteNonQuery()
dbConnect.Close()

Enjoy
-Animal Mother

This article has been dead for over six months. Start a new discussion instead.