I am trying to update a database record when a user amends it. I have a textbox called 'txtColsTextBox' which stores the value and a button called 'btnSaveExit'. On the button click, I need to update the db with new value.
How would I do this based on my code. I am thinking , i need to use me.validate function but not sure how to code. Thanks
Dim connetionString As String
Dim oledbCnn As OleDbConnection
Dim oledbCmd As OleDbCommand
Dim sql As String
connetionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\domain\storage.accdb"
sql = "SELECT Cols FROM Racks Where [Rack code] = '" & buttonName & "'"
oledbCnn = New OleDbConnection(connetionString)
Try
oledbCnn.Open()
oledbCmd = New OleDbCommand(sql, oledbCnn)
Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
While oledbReader.Read
'MsgBox(oledbReader.Item(0))
txtColsTextBox.Text = oledbReader.Item(0)
End While
oledbReader.Close()
oledbCmd.Dispose()
oledbCnn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
EDIT: Code to Update db
Private Sub btnSaveExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveExit.Click
Try
connetionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\domain\storage.accdb"
oledbCnn.Open()
oledbCmd = New OleDbCommand(sql, oledbCnn)
oledbCmd.CommandText = "UPDATE [Racks] SET [Cols] VALUES (?)"
oledbCmd.Parameters.AddWithValue("@Cols", txtColsTextBox.Text)
oledbCmd.ExecuteNonQuery()
MessageBox.Show("Record succesfully updated" + txtColsTextBox.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
oledbCnn.Close()should be in a finally block.