I am trying to update a record in Access through VB.NET. This is my code thus far. The objective is to search a barcode, it returns the data associated with it and the user is able to change the data and save the updated data. I have everything except the update part. This is my save button with the update code. I hope I made this clear.
EDIT: Sorry. I forgot to tell you all my problem. It returns 0 rows affected, no error message.
Private Sub btnEditProductSave_Click(sender As System.Object, e As System.EventArgs) Handles btnEditProductSave.Click
'Creates Transaction and Connection Objects
Dim Transaction As OleDb.OleDbTransaction = Nothing
Dim connection As OleDb.OleDbConnection = Nothing
Try
'Assign the Connection
connection = New OleDb.OleDbConnection(My.Settings.POS_NEWConnectionString)
'Opens the Connection and begins the Transaction
connection.Open()
Transaction = connection.BeginTransaction
Dim barcode As String = txtEditProductBarcode.Text
Dim name As String = txtEditProductName.Text
Dim supplier As String = cbEditProductSupplier.SelectedValue
Dim sell As Decimal = Val(txtEditProductSellPrice.Text)
Dim quantity As Integer = numQuantity.Value
Dim discount As Decimal = Val(txtEditProductDiscount.Text)
Dim department As Integer = Val(txtEditProductDepartment.Text)
Dim SQL As String = "UPDATE PRODUCT SET ProductName = @productName, SupplierId = @supplier, SellPrice = @sellPrice, Quantity = @quantity, DiscountPrice = @discount, DepartmentNumber = @dept WHERE ProductBarcode = @barcode"
'Creates the Command for the Transaction
Dim CT1 As New OleDb.OleDbCommand
'Assigns a connection and transaction to a new command object
CT1.Connection = connection
CT1.Transaction = Transaction
CT1.Parameters.AddWithValue("@barcode", barcode)
CT1.Parameters.AddWithValue("@productName", name)
CT1.Parameters.AddWithValue("@supplier", supplier)
CT1.Parameters.AddWithValue("@sellPrice", sell)
CT1.Parameters.AddWithValue("@quantity", quantity)
CT1.Parameters.AddWithValue("@discount", discount)
CT1.Parameters.AddWithValue("@dept", department)
CT1.CommandText = SQL
Dim number As Integer = CT1.ExecuteNonQuery
MsgBox(number & " of row were affected")
CT1.Dispose()
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Parameters in OleDb are in index order, not name.
Try it this way (barcode parameter last):
You also have a
BeginTransactionbut no commit.It should look something like this: