I am using Vb.Net to update my access database with some textboxes. This is the code that I have which doesn't give any error when the 'update' button is clicked in the form, but nothing happens either. For some reason the database doesn't get updated. I'm not sure what I'm doing wrong.
Dim pro As String
Dim connstring As String
Dim command As String
Dim myconnection As OleDbConnection = New OleDbConnection
pro = "Provider =Microsoft.ACE.OLEDB.12.0; data source= C:\Users\Hamza\Documents\POS system1.accdb"
connstring = pro
myconnection.ConnectionString = connstring
myconnection.Open()
Dim Str = "Update Customers SET FirstName=?, LastName=?, Address=?, PhoneNo=?, Points=? WHERE CustID=?"
Dim cmd = New OleDbCommand(Str, myconnection)
cmd.Parameters.AddWithValue("@CustID", CustIDTextBox)
cmd.Parameters.AddWithValue("@FirstName", First_NameTextBox)
cmd.Parameters.AddWithValue("@LastName", Last_NameTextBox)
cmd.Parameters.AddWithValue("@Address", AddressTextBox)
cmd.Parameters.AddWithValue("@PhoneNo", Phone_noTextBox)
cmd.Parameters.AddWithValue("@Points", PointsTextBox)
cmd.ExecuteNonQuery()
MsgBox("Updated!")
You can pass the connection string directly to the constructor of the connection.
OleDb (used with Access) does not care about the names of parameters. The order that they appear in the sql string must match the order that they are added to the parameters collection. Thus, move the CustID to the end of the parameters being added because it is the last thing in the sql string.
I was very glad to see that you are using parameters. The Add
method gives you a much better chance of sending the proper datatype to the database. I wasn't sure if the TextBox variables were text boxes that needed the Text
property or variables you had set. I had to guess at the datatypes. Check your database for the actual types.
Connections and Commands need to be disposed. Using...End Using
blocks accomplish this for us. It also closes the connection. You didn't close your connection at all.
Don't open the connection until directly before the Execute...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Str = "Update Customers SET FirstName=?, LastName=?, Address=?, PhoneNo=?, Points=? WHERE CustID=?"
Dim pro = "Provider =Microsoft.ACE.OLEDB.12.0; data source= C:\Users\Hamza\Documents\POS system1.accdb"
Using myconnection As New OleDbConnection(pro),
cmd As New OleDbCommand(Str, myconnection)
cmd.Parameters.Add("@FirstName", OleDbType.VarChar).Value = First_NameTextBox.Text
cmd.Parameters.Add("@LastName", OleDbType.VarChar).Value = Last_NameTextBox.Text
cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = AddressTextBox.Text
cmd.Parameters.Add("@PhoneNo", OleDbType.VarChar).Value = Phone_noTextBox.Text
cmd.Parameters.Add("@Points", OleDbType.Integer).Value = CInt(PointsTextBox.Text)
cmd.Parameters.Add("@CustID", OleDbType.Integer).Value = CInt(CustIDTextBox.Text)
myconnection.Open()
cmd.ExecuteNonQuery()
End Using
MsgBox("Updated!")
End Sub