[RESOLVED] [VB 2003] DateTime cast type
Hi,
I am having a problem turning nothing into a datetime. Here is the line I am using:
VB Code:
Dim DtApproved As DateTime = IIf(Me.lblRtngApprovedDate.Text = "", DateTime.MinValue, CType(Me.lblRtngApprovedDate.Text, DateTime))
The date is stored as the text of a label (that's just the way it is, I didn't design this, I just gotta work with it). So, if the text = "", I want DtApproved to equal DateTime.MinValue, else I want it to convert the text in the label to a date.
The second part is working, but it is crashing when the label's text equals "". Any ideas on how to fix this?
Thanks to all
Re: [VB 2003] DateTime cast type
just so you know, DateTime.MinValue is equal to January 1, 0001
Not sure if that's what you want or not.
Anyway, I never recommend using inline iff statements.. It is generic, so it returns object types, so they need to be casted correctly.. Basically I think its more trouble than the line of code it saves...
anyway.. this should work
VB Code:
Try
Dim DtApproved As DateTime = DateTime.Parse(IIf(Me.lblRtngApprovedDate.Text = "", DateTime.MinValue.ToString, Me.lblRtngApprovedDate.Text).ToString)
Catch ex As Exception
MessageBox.Show("Invalid Entry")
End Try
Re: [VB 2003] DateTime cast type
Thanks a lot, thats works perfectly. And thanks for the advice on not using IIf.