String not recognized as valid DateTime in IsDate funtion

jtyoder

Newcomer
Joined
Feb 17, 2003
Messages
4
When executing code to determine if a text box contains text that's convertable to a date I get the following error :



A first chance exception of type 'System.FormatExeption' occured in mscorlib.dl.

Additional information : String was not recognized as a valid DateTime.


This happens when executing the code :

If IsDate(txtDate.Text) Then .....

Oddly enough, when I break the program and run this command in the immediate window the command comes back with a boolean as I expect.

Anyone have any ideas?

Thanks

James
 
Actually, my above statements assumed too much. I attempted to input null (Nothing) and empty (String.Empty) values and the function still returned correctly. I have a feeling that your problem is elsewhere, possibly in that surrounding block of code. If you could post the remainder of your code I'd like to look it over.
 
Not much to show, but here it is :

Visual Basic:
    Private Sub txtPullDate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPullDate.TextChanged
    'Actions to take when the txtPullDate box changes value
If IsDate(txtPullDate.Text.Trim) Then
  txtPullDate.Text = "The If statment is busted!"

End If
    '---
      End Sub
 
Last edited by a moderator:
Where is this IsDate function? The only one I could find in the
object browser was in the MS compat. library, in with the likes of
'UBound' et al.

My IsDate function looks something like this:
Visual Basic:
Public Function IsDate(ByVal d As String)
  Try
    Dim dummy As Date = Date.Parse(d)
    Return True
  Catch
    Return False
  End Try
End Function
 
IsDate() is a Visual Basic .NET function. You can find its documentation under the language and reference portion of the .NET SDK. I'm guessing it's a mapping to System.Convert or Date.Parse(), using a method similar to yours, above.
 
It's in the 'Visual Basic Language Reference' portion of the MSDN,
along with UBound, QBColor, RGB and others.
I don't think it's a .NET function at all.
 
Ah, I see what you mean now. D'oh! Still, is it not better to use a
self-written function over a Microsoft-compat. library function, since
they can only be accessed (easily) from within VB, whereas that
function could be used in C# if it was required?
 
James, one thing you may consider is, instead of using the TextChanged event maybe use the Leave Event. As the user may want to enter the month in words, ie. Feb 2 2003
 
Back
Top