I have a calendar in an aspx page, if the user doesn't select a date it puts 0001/01/01 into the database. How can I specify another date, like put today's date instead if no date is chosen?
There are couple of ways. If it is the Calendar control, you can programatically set the selected date on Page_Load to Today's date. That way even if the user doesn't select a date, you get the default selected date.
Second option would be, before inserting to the database - you can check if the selected date is <=DateTime.MinDate, and set current date in the object.
您可以用convert(varchar(10),getdate(),101)
替换“ 0001/01/01”
There are two ways: 1) If the date field is empty then assign the date field value as system.datetime.now so that while you are passing it to the procedure you can insert the current date.
for example txtdate is field where date is assigned
if(txtdate.text == "")
{
txtdate.text = system.datetime.now;
}
pass this to the procedure.
2)in the database level,check whether the date is empty or not If it is empty then assign date as getdate()
for example
create procedure test(@date varchar(50) = '')
as
begin
if(@date <> '')
begin
-- your code goes here
end
else if(@date = '')
begin
getdate() value to @date
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.