Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert date/time into Access database

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!

like image 388
Sara Avatar asked Apr 20 '12 03:04

Sara


1 Answers

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()

like image 62
Daniel Wolfe Avatar answered Oct 14 '22 08:10

Daniel Wolfe