I have a Visual Basic project using Access database.I run a query but i don’t see any new data on my database table.I don’t have any exception or error.Instead of this the success messagebox is shown.
Here is my code:
Dim ID As Integer = 2
Dim TableNumber As Integer = 2
Dim OrderDate As Date = Format(Now, "General Date")
Dim TotalPrice As Double = 100.0
Dim ConnectionString As String = "myconnectionstring"
Dim con As New OleDb.OleDbConnection(ConnectionString)
Try
Dim InsertCMD As OleDb.OleDbCommand
InsertCMD = New OleDb.OleDbCommand("INSERT INTO Orders([ID],[TableNumber],[OrderDate],[TotalPrice]) VALUES(@ID,@TableNumber,@OrderDate,@TotalPrice);", con)
InsertCMD.Parameters.AddWithValue("@ID", ID)
InsertCMD.Parameters.AddWithValue("@TableNumber", TableNumber)
InsertCMD.Parameters.AddWithValue("@OrderDate", OrderDate)
InsertCMD.Parameters.AddWithValue("@TotalPrice", TotalPrice)
con.Open()
InsertCMD.ExecuteNonQuery()
MessageBox.Show("Successfully Added New Order",
"Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
con.Close()
Catch ex As Exception
'Something went wrong
MessageBox.Show(ex.ToString)
Finally
'Success or not, make sure it's closed
If con.State <> ConnectionState.Closed Then con.Close()
End Try
What is the problem?
One of two things is happening: Either the data is not getting updated, or it’s getting updated and you’re missing it when you look for it afterward. ExecuteNonQuery normally returns the number of rows acted on. If it’s zero, then it’s possible it’s failing without an exception. This might have something to do with a duplicate ID.
If it’s inserting successfully, then you aren’t seeing the new record for some reason. Maybe you’re looking in the wrong place, or maybe the record isn’t turning up in a query for some reason. I don’t know of anything with “on duplicate update” by default, but if Access has that it could cause the problem.