Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

I have an access database with datetime column.
I use this query to insert values in it
SQL
Insert into docu(doc_name,doc_category,doc_type,doc_format,doc_filename,date_add,share_doc,publish_by) values ('" & TextBox1.Text & "','" & cat & ", " & tipe & ",'" & ComboBox3.Text & "','" & nama_file & "'," & Format(Now, "dd/mm/yyyy") & "," & isshare & "," & id_user & " )")
the problem is that the date is not displayed correctly in ListView
shows 01/01/1900

can anyone help
Thanks a lot
Posted
Updated 20-Oct-12 5:36am
v2

1 solution

First off, don't do that. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

This may also solve your problem, since instead of converting a DateTime into a string and then sendign it to SQL which converts it back to a DateTime (with the possible errors that involves) you send the DateTime directly, and it is always stored as what you sent - there is no interpretation going on.

(And I'm going to ignore the error in the format string which is sending the current minutes to SQL...)

VB
Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
        com.Parameters.AddWithValue("@C1", myValueForColumn1)
        com.Parameters.AddWithValue("@C2", myValueForColumn2)
        com.ExecuteNonQuery()
    End Using
End Using
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900