This is the code that I am trying to run. It will run without errors, but it does not update my database.
It will work when it is not Parameterized, but when I add parameters in it starts acting up. Here is the problematic code.
Public Sub updateItem()
Dim sqlConnection1 As New OleDb.OleDbConnection(dbProvider + dbSource)
Dim cmd As New OleDb.OleDbCommand
cmd.CommandText = "Update Inventory set PartNumber='@PartNumber', Brand='@Brand', PartDescription='@PartDescription', PartCost=@PartCost, InventoryOnHand=@InventoryOnHand, PartSupplier='@PartSupplier' where PartNumber = '@PartNumMatch' and Brand = '@PartManMatch';"
cmd.Parameters.AddWithValue("@PartNumber", partNumberText.Text().ToUpper())
cmd.Parameters.AddWithValue("@Brand", ManufacturerText.Text())
cmd.Parameters.AddWithValue("@PartDescription", partDescriptionText.Text())
cmd.Parameters.AddWithValue("@PartCost", Convert.ToDouble(partCostText.Text()))
cmd.Parameters.AddWithValue("@InventoryOnHand", Convert.ToInt32(quantityText.Text()))
cmd.Parameters.AddWithValue("@PartSupplier", partSupplierText.Text())
cmd.Parameters.AddWithValue("@PartNumMatch", partNumberText.Text().ToUpper().Trim())
cmd.Parameters.AddWithValue("@PartManMatch", ManufacturerText.Text().ToUpper().Trim())
cmd.CommandType = CommandType.Text
cmd.Connection = sqlConnection1
Try
sqlConnection1.Open()
cmd.ExecuteNonQuery()
sqlConnection1.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
sqlConnection1.Close()
End Try
'SQl statement to try to update the selected row's data matched against the database.
'update listview here.
End Sub
I am almost sure that the syntax is correct because my insert works. Here is the code to my insert.
Private Sub addItem()
'SQL statement here to add the item into the database, if successful, move the information entered to listview.
Dim sqlConnection1 As New OleDb.OleDbConnection(dbProvider + dbSource)
Dim cmd As New OleDb.OleDbCommand
'Dim reader As SqlDataReader
cmd.CommandText = "Insert into Inventory ([PartNumber], [Brand], [PartDescription], [PartCost], [InventoryOnHand], [PartSupplier]) values (@PartNumber, @Brand, @PartDescription, @PartCost, @InventoryOnHand, @PartSupplier);"
cmd.Parameters.AddWithValue("@PartNumber", partNumberText.Text().ToUpper().Trim())
cmd.Parameters.AddWithValue("@Brand", ManufacturerText.Text().ToUpper().Trim())
cmd.Parameters.AddWithValue("@PartDescription", partDescriptionText.Text().Trim())
cmd.Parameters.AddWithValue("@PartCost", partCostText.Text())
cmd.Parameters.AddWithValue("@InventoryOnHand", quantityText.Text())
cmd.Parameters.AddWithValue("@PartSupplier", partSupplierText.Text().Trim())
cmd.CommandType = CommandType.Text
cmd.Connection = sqlConnection1
Dim found As Boolean = False
Try
sqlConnection1.Open()
cmd.ExecuteNonQuery()
MessageBox.Show(cmd.CommandText)
sqlConnection1.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
sqlConnection1.Close()
End Try
End Sub
I know that the where clause is right, I have hard-coded the value's and I have also pushed the value's being compared to message box's and compared them directly to the information in the database.
Thanks in advance for any and all opinions and I hope we can get it figured out.
The parameters placeholders should not be enclosed in single quotes
cmd.CommandText = "Update Inventory set PartNumber=@PartNumber, Brand=@Brand, " +
"PartDescription=@PartDescription, PartCost=@PartCost, " +
"InventoryOnHand=@InventoryOnHand, PartSupplier=@PartSupplier " +
"where PartNumber = @PartNumMatch and Brand = @PartManMatch;"
You don't need to do that, it only confuses the code that tries to replace the parameter placeholder with the actual value. They will be treated as literal strings