Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm making a button that when i click, it record a data to access table and i can view it on datagridview. i have the table of date and time separated. so it shown date column with the right date. but the time column shown 1/1/2001 6:39:56pm. i'm using long format for time at access table
is there anything wrong with my code?

VB
Sub catat()
     sql = "insert into [userhistory] ([username],[date recorded],[time recorded],[activity],[reg number]) values (?,?,?,?,?)"
     kns.Open()
     Using cmd As New OleDbCommand(sql, kns)
         cmd.Parameters.AddWithValue("@p1", LOGIN.USERNAME.Text)
         cmd.Parameters.AddWithValue("@p2", System.DateTime.Today.ToString)
         cmd.Parameters.AddWithValue("@p3", TimeOfDay.ToString)
         cmd.Parameters.AddWithValue("@p4", "Deleting User")
         cmd.Parameters.AddWithValue("@p5", dg.CurrentRow.Cells(0).Value.ToString)
         cmd.ExecuteNonQuery()
     End Using
     kns.Close()
 End Sub
Posted
Updated 25-Jan-15 0:39am
v2
Comments
Richard MacCutchan 25-Jan-15 7:15am    
Why are you storing dates and times as separate fields, and as string values, in your database? That is completely wrong and will lead to many problems in the future.
vendir 26-Jan-15 4:53am    
i store it in separate fields so the user can find the data using date only.
any suggestion?

1 solution

Please, don't store dates and times as strings - not in any database!
Instead, store a date and time together in a DATETIME field, and then return just the information you want, formatted correctly, for your table.

String based dates in particular are a nightmare to work with, because they will be stored in a format which depends on the settings for the computer that inserted the data: so if the settings change, (say from a user who prefers European date format dd/MM/yy to a US format MM/dd/yy or Japanese yy/MM/dd) then the data in your DB becomes inconsistent.
This causes horrible problems some considerable time after the problem occurred, and it can be a real nightmare to fix the data.

Storing as DATETIME values avoids all this.

So change your DB, change to code to reflect that, and then look at formatting the output - which is the really, really, easy bit!
 
Share this answer
 
Comments
vendir 25-Jan-15 9:30am    
i store it in same field
i change it to:

cmd.Parameters.AddWithValue("@p2", now)

it shown error on my insert command

i change it to:

cmd.Parameters.AddWithValue("@p2", DateTime.Now.Date)

it only save date without the time
any help? i'm rookie on this thing
OriginalGriff 25-Jan-15 10:26am    
A DATETIME value always as both a date and a time: DateTime.Now.Date just sets the time portion to midnight.
Just save it as a single column in your DB - a DateTime column - and separate it for display when you want to present it to the user.

Does that make sense?
vendir 25-Jan-15 23:05pm    
yes it does make sense and i already tried it
the problem is datetime.now gave an erroron my insert command
what code should i use?
OriginalGriff 26-Jan-15 2:19am    
And what's the error?
vendir 26-Jan-15 4:51am    
Data type mismatch in criteria expression
it underlined on cmd.executenonquery?

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