You can use nullable types, if using 2.0, but you will have to test the
value before you bind it, as Nothing/null blows up when bound to a control.
As far as conversion, .TryParse() is a safer method of setting a DateTime,
as users sometimes do not respect dates.
If you want to force dates, you can make the control so it cannot be filled
in and use a calendar control. Or you can use an AJAX masked edit (in the
AJAX control toolkit).
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss
or just read it:
http://gregorybeamer.spaces.live.com/
*************************************************
| Think outside the box!
|
*************************************************
"James" <Ja***@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.com...
Hello,
I am trying to create a Function to test TextBoxes and convert the Text to
DateTime if not empty.
My code is :
Public Shared Function GetDateTime(ByVal DateValue As String) As String
If Not String.IsNullOrEmpty(DateValue) Then
Return DateTime.Parse(DateValue)
Else
Return Nothing
End If
End Function
Then in a OnClick command I have:
CurrentMember.StartDate = GetDateTime(txtStartDate.Text)
It works ok when there is a date but if its empty I get an error message:
System.InvalidCastException: Conversion from string "" to type 'Date' is
not
valid.
If I do CurrentMember.StartDate = Nothing it works fine so no problem with
Null values in the field.
Does anyone have any suggestions as this has puzzled me for too long now.
Thanks in advance
James