cmdUpdate.CommandText = "UPDATE Products2 " +
" SET ProductName = '" + txtProductName.Text + "', " +
" Description = '" + txtDescription.Text + "' , " +
" Quantity = '" + txtQuantity.Text + "' , " +
" CriticalLevel = '" + txtCriticalLevel.Text + "' , " +
" PurchasePrice = '" + txtPurchasePrice.Text + "' , " +
" SellingPrice = '" + txtSellingPrice.Text + "' , " +
" ProductStatus = '" + cboStatus.Text + "' , " +
" Created = '" + dtpCreated.Value.Date.ToString() + "' , " +
" LastModified = '" + dtpLastModified.Value.Date.ToString() + "' , " +
"WHERE (ProductNo=" + txtProdNo.Text + ");";
How do I fix this code?
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'WHERE'.'
The smallest problem you have is that last comma before the
WHERE.The larger one is the huge security hazard caused by concatenating user input into your SQL statement.
Instead, use parameters. That will also make your code much more readable:
Why is SQL injection still a thing?