jtyoder Posted February 17, 2003 Posted February 17, 2003 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 Quote
*Gurus* Derek Stone Posted February 17, 2003 *Gurus* Posted February 17, 2003 Make sure that the entered string isn't null or equivalent to the String.Empty field. Other than that, that line of code shouldn't be giving you any problems. Quote Posting Guidelines
*Gurus* Derek Stone Posted February 17, 2003 *Gurus* Posted February 17, 2003 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. Quote Posting Guidelines
jtyoder Posted February 17, 2003 Author Posted February 17, 2003 (edited) Not much to show, but here it is : 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 Edited February 17, 2003 by divil Quote
*Gurus* Derek Stone Posted February 17, 2003 *Gurus* Posted February 17, 2003 That code block can (and does) work fine. I just verified it, so I don't know what to tell you. Quote Posting Guidelines
*Experts* Volte Posted February 17, 2003 *Experts* Posted February 17, 2003 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:Public Function IsDate(ByVal d As String) Try Dim dummy As Date = Date.Parse(d) Return True Catch Return False End Try End Function Quote
*Gurus* Derek Stone Posted February 17, 2003 *Gurus* Posted February 17, 2003 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. Quote Posting Guidelines
*Experts* Volte Posted February 17, 2003 *Experts* Posted February 17, 2003 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. Quote
*Gurus* divil Posted February 17, 2003 *Gurus* Posted February 17, 2003 The function VolteFace posted is exactly what the VB.NET IsDate function does. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Volte Posted February 17, 2003 *Experts* Posted February 17, 2003 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? Quote
Moderators Robby Posted February 17, 2003 Moderators Posted February 17, 2003 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 Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.