You are missing a parameter in your parameters collection. There are four parameters placeholders (the ?) but you add only three parameters to the collection.
You need to add the last one, the one used in the WHERE clause.
But a part from this trivial error, you need to pay a lot of attention when using AddWithValue
because that method has unexpected behavior when you don't specify correctly the parameter value.
In your case, you pass always strings as the parameter value, but I am pretty sure that your database fields are not all strings.
In this context AddWithValue
has no way to know if a particular parameter will be used to update a numeric, a datetime or a boolean field and thus looks at the datatype of the value to determine the datatype of the parameter.
This could easily cause a DataType Mismatch exception or wrong values inserted, in particular with dates and decimal values. These values are sent to the database engine represented as strings in the localization of the client program but they could be incompatible with the code used by the database to convert the strings back to the correct type expected by the underlying columns.
See more info in this blog article Can we stop to use AddWithValue already?
Better to use always the specific implementation of the Parameters.Add
method that allows you to specify the exact datatype of the receiving column.
Of course the parameter value should be of the correct datatype.
Dim str As String
str = "UPDATE Rooms SET [Room Type] = ?, Price = ? WHERE [Room Number] = ?"
Using con1 = New OleDbConnection("......")
Using cmd = New OleDbCommand(str, con1)
con1.Open()
cmd.Parameters.Add("@p1", OleDbType.VarWChar).Value = TextBox3.Text
cmd.Parameters.Add("@p2", OleDbType.Decimal).Value = Convert.ToDecimal(textBox1.Text)
cmd.Parameters.Add("@p3", OleDbType.Integer).Value = Convert.ToInt32(textBox2.Text)
cmd.ExecuteNonQuery()
End Using
End Using
Notice that I have changed your query to work with just three parameters because, as far as I know, there is no need to change the Room Number
value to the same value used in the WHERE clause
Of course the exact OleDbType
to use in each Add should match the real datatype of your column.