Gary Yee
Gary Yee

Reputation: 17

DateTimePicker Format

I have a problem with the DateTimePicker. I am new to C# and developing a standalone application with MS Access 2007 as my DB.

I have a DateTimePicker (dtp_btime) with Format as "Time" and Custom Format "hh:mm tt". No doubt it can display as "10:30 pm". But when I click on save it obviously saved both the Date and Time in the DB table.

 cmd.CommandText = "insert into Booking(cname, bdate, btime, ccontact, sname) Values('" + txt_cname.Text + "','" + dtp_bdate.Value.Date + "','" + dtp_btime.Value + "','" + txt_ccontact.Text + "','" + txt_sname.Text + "')";

How can I only save the time without the date from the DateTimePicker?

Upvotes: 0

Views: 2343

Answers (2)

Dylan Smith
Dylan Smith

Reputation: 22235

It's not a problem with the DateTimePicker, it's a problem with the database datetime type. The database stores a date and time together. You can force it save with a date of 01/01/0001 or something like that for every column if you want. I don't know if Access has a datatype that only stores time, but I would doubt it.

Upvotes: 0

Bala R
Bala R

Reputation: 108937

If it's a text field in access, you should be able to use

dtp_btime.Value.ToString("hh:mm tt")

If it's not, then just store date and time as-is and display just the time portion in your reports or wherever it's displayed.

Upvotes: 0

Related Questions