Click here to Skip to main content
15,886,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm using a Calendar control in a formview, and having problems only when no date is selected,in sql one default date is comming.this field is not a mandortary field,when we select date then only value want to come otherwise we want null vale. How can i fix this issue.
Posted
Comments
DamithSL 26-May-15 11:55am    
update the question with related code samples
ZurdoDev 26-May-15 11:58am    
You need to explain where you are stuck. This is not clear.
ZurdoDev 26-May-15 12:04pm    
This does not tell me anything. What do you need?
ZurdoDev 26-May-15 12:17pm    
Then check for 1/1/1900 and pass in DBNull.Value instead.
Sergey Alexandrovich Kryukov 26-May-15 17:36pm    
This is dirty. Too bad this control works this way, but even then it should be DateTime.MinValue, which is too bad anyway. Please see my answer.
—SA

1 solution

I think the control System.Web.UI.WebControls.Calendar is defective in this respect. MSDN documentation says that the default value is DateTime.MinValue. This is a shame, really. It means this value, otherwise valid, is used in this control is roughly equivalent to "no-value" or "unset" status. This is wrong. Please see:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar.selecteddate%28v=vs.110%29.aspx[^].

Note that you cannot have null for System.DateTime type, because it is a value type, not a reference type. You could use the nullable type System.DateTime? instead. Too bad, this is not how this control works.

Any practical work-around? Well, you can subclass this control, create a derived class "NullableCalendar". Add additional property of the System.DateTime?. Internally, handle the event SelectionChanged:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar.selectionchanged%28v=vs.110%29.aspx[^].

In the handler, when some "real" date is selected, assign your nullable property to SelectedDate. This way, you will be able to tell "never selected" status of the control by checking this property for null from the "really selected". Think about some custom rendering (such as background color) which would indicate this "never selected" status visually.

—SA
 
Share this answer
 
v2
Comments
Unni R 26-May-15 18:17pm    
This field is not a mandatory
Sergey Alexandrovich Kryukov 26-May-15 20:05pm    
Sorry, I don't understand. What field (I did not mention any) and how is it related to your problem?
—SA

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