Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Format for NULL or Empty 1

Status
Not open for further replies.

taysys

Programmer
Dec 7, 2001
41
0
0
US
I am trying to format a SQL date in a form. When the date is first created it is <null>. If it is updated, I am able to format it fine. But am fighting what happens when the date is cleared out ie. txtDate.text = &quot;&quot;... to try to work around some of the problems I tried using .minvalue, but seem to be on the wrong track.

I am using Data binding as follows:

Dim dbnPlaceOpened As New Binding(&quot;Text&quot;, ds, &quot;Placements.DatePlacementStart&quot;)

AddHandler dbnPlaceOpened.Format, AddressOf DateToString
AddHandler dbnPlaceOpened.Parse, AddressOf StringToDate
txtPlaceOpened.DataBindings.Add(dbnPlaceOpened)

Protected Sub DateToString(ByVal sender As Object, ByVal e As ConvertEventArgs)
If Not IsDBNull(e.Value) Then
If CType(e.Value, DateTime) = Date.MinValue Then
e.Value = String.Empty
Else
e.Value = CType(e.Value, DateTime).ToShortDateString
End If
Else
e.Value = String.Empty
End If

End Sub

I have tried different styles in the &quot;format&quot; method but am getting an Invalid Cast from &quot;&quot; to Date. Without getting too convoluted, any suggestions??

Thanks,

Bob Taylor
 
Bob,

You have to use DBNull here, try

DBNull.Value instead of &quot;&quot;.
So the code would look like

e.Value = DbNull.Value

-Kris
 
Dang, Kris... didn't I try that first? Guess, not... since it works.

Thanks!

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top