catchyourwaves
catchyourwaves

Reputation: 1

Date and Time in datagridview

somebody help me with the problem that every time i save or update a data the date is working fine but the time is always 12:00AM. the time should be the time when I input the data and be saved into the database.

this is my code for the update

        Dim i As Short
    con.Open()
    Using com As New SqlClient.SqlCommand("UPDATE Information set FiveS_Date = '" & DateTimePicker1.Text & "',FiveS_Score = '" & TextBox2.Text & "',FiveS_Total = '" & TextBox3.Text & "' ,FiveS_Percentage = '" & TextBox4.Text & "' WHERE Id='" & id & "'", con)
        i = com.ExecuteNonQuery()
    End Using
    con.Close()
    If (i > 0) Then


        MsgBox("Training updated successfully", MsgBoxStyle.Information)
    End If

please help.

Upvotes: 0

Views: 1937

Answers (5)

user4267260
user4267260

Reputation:

To insert time into database you need to use

DateTimePicker1.Value 

instead of

DateTimePicker1.Text

Upvotes: 0

Wildan Syahidillah
Wildan Syahidillah

Reputation: 43

For starters, DateTimePicker1.Text will only give you the String that is displayed in the DateTimePicker, by default the DateTimePickers format will only show you the Date and by default the time will be 12:00 AM. You can change the DateTimePicker format to show time by changing the Format field in the Properties Window of the DateTimePicker to Time. DateTimePicker to Time format

Or by your sentence you want to save the date selected combined with the time you saved the data to the database, just make a new DateTime variable and insert the DateTimePicker Dates as values to that variable and the time from DateTime.Now() function which will return the current date and time.

Dim now As DateTime = DateTime.Now()
Dim myDate = new DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day, now.Hour, now.Minute, now.Second, now.Millisecond)

Upvotes: 0

Beldi Anouar
Beldi Anouar

Reputation: 2180

Try this :

FiveS_Date ='" + DateTime.Parse(datetxt.Text).ToString("dd/MM/yyy") +"'

Upvotes: 0

Shukri Gashi
Shukri Gashi

Reputation: 535

You should use parameters to avoid the problem of data conversion when you use ADO.NET. This helps you also with different cultures in your application.

Anyway, DateTimePicker1.Valuewill give you also the time, but if you select a date which is different from default value of DateTimePicker (which is Now), then DateTimePicker will always give you the time "12:00". You have to format it to show time also and to have the possibility to set it (example "MM/dd/yyyy HH:mm").

Upvotes: 1

OSKM
OSKM

Reputation: 728

Try changing DateTimePicker1.Text to DateTimePicker1.Value.ToString

.Text just returns what is visible (date only) whereas .Value returns the current time as well as the date displayed.

Upvotes: 0

Related Questions