Current Date

tbpowers

Active member
Joined
Dec 12, 2011
Messages
41
Programming Experience
Beginner
I have a create_date column in my database, which is non-nullable. I want to create a text box or something on my form, which shows the current date time when a new record is created and write this back to the create_date column in my database. I do not want the user to be able to have access to the date, but would like it to show on the form. All I see in VS is a date picker. Any ideas?
 
What do you usually use when you want to show data to the user without their editing it? The obvious choices are a Label or possibly a TextBox with its ReadOnly property set to True. Both the Label and TextBox controls display a String to the user, so you need to get the current date and time, convert it to a String and then display it.

That said, let's say that the user opens a new record in your app and you display the current date and time. They then leave the app open for 10 hours before finally saving the record. Do you want to save the date and time displayed in the app or the date and time they saved the record? Do you really need to display the date and time in the new record at all, given the the user can simply look at the clock to know what time it is?
 
Users will need to see the create date when viewing saved records in the application. The create date in the database table is non-nullable, so I need to populate this field in my app so I can pass it when the record is saved.

I have another question. When I created my SQL Server database I setup the create date with a default of getdate(). I assumed when I created my app and saved a record that the back-end (getdate()) would handle the date. However, that is not the case. If I try to save the record without a create date in the app I get the "Null" error. Why is that?
 
Because the date column of the datatable is set to not allow nulls ?

the getdate() in SQLS will only be used to provide a value if you don't provide one in your INSERT statement...

Why not just edit the insert so that it doesnt send a date at all?

old:
INSERT INTO tbl (thedate, thename) VALUES (@thedate, @thename)

new:
INSERT INTO tbl (thename) VALUES (@thename)


Just because it's in the SELECT doesnt mean it has to be in the insert

Like JMC says tho... sort out in your head what the create date opf a record is.. Is it when the user presses new in the app, or is it when they save? If former, set the date yourself. If latter, have the db do it with getdate()
 
Back
Top