I'm using ASP.NET/VB and I'm trying to insert a date and time into an Access date/time field, but I'm getting an error (Data type mismatch in criteria expression). Here's a simplified version of my code:
Dim myDate As Date = Now()
Dim myIns1 As New OleDbCommand("INSERT INTO tableCourse (date_submitted) VALUES (?)", myConn)
myIns1.Parameters.AddWithValue("@myDate", myDate)
myIns1.ExecuteNonQuery()
Not sure why I'm getting the error, and not quite sure if this is even the correct way to approach inserting the current date. From looking at other similar questions it appears there are a few different ways to do this, but my technical knowledge is kind of limited so I'm having a hard time figuring it out (in other words deciphering the answers that use technical terms I know nothing about).
Thanks in advance!
You could skip the parameter altogether and just write the NOW function directly into the query.
Dim myIns1 As New OleDbCommand("INSERT INTO tableCourse (date_submitted) VALUES (NOW())", myConn)
myIns1.ExecuteNonQuery()
This answer (the one above) is probably the better solution, but the reason why the original code didn't work is because the parameter wasn't named. You could try this:
Dim myDate As Date = Date.Now
Dim myIns1 As New OleDbCommand("INSERT INTO tableCourse (date_submitted) VALUES (@myDate)", myConn)
myIns1.Parameters.AddWithValue("@myDate", myDate)
myIns1.ExecuteNonQuery()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With