Facing issue in Date format (string was not recognized as a valid datetime in vb.net)
Hi There,
i am facing issue in date format. please help me to fix this.
this below content is the raw data and i am pulling that from csv file
---------------------------------------------------------------------------------------------------
Incident ID Title Status Open Time
IM17911585 IPG:NA Status Of Order 24IH42151001 (SBB2486382) Open 3/27/2014 9:10
---------------------------------------------------------------------------------------------------
Dim opentime As DateTime ' = Nothing ' open Date
Dim tmpArray() As String = Regex.Split(hpsm, ",")
If tmpArray(0) = "" Then
Exit While
End If
id = Trim(tmpArray(0))
title = tmpArray(1)
status = tmpArray(2)
opentime = DateTime.Parse(tmpArray(3))
When i run the program the open time is showing error and it is not picking the format correctly. please refer the below error.
Database Error: System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.DateTime.Parse(String s)
Please help me to fix this.
Re: Facing issue in Date format (string was not recognized as a valid datetime in vb.
Use TryParseExact instead... that might help... but before that, you may want to debug your code and step through it to make sure that your data is what you think it is... the sample data you gave doesn't match the code. You're splitting on a comma... only there aren't any in the data... so your tempArray only ends up with a single element.
Also, mostly out of curiosity, is there a reason you're using the RegEx split rather than just the string split function? I mean if you were splitting off of something complex, I'd get it, but since it appears to be just off the lowly comma, regex seems like overkill to me.
-tg
Re: Facing issue in Date format (string was not recognized as a valid datetime in vb.
Quote:
Originally Posted by
techgnome
Use TryParseExact instead... that might help... but before that, you may want to debug your code and step through it to make sure that your data is what you think it is... the sample data you gave doesn't match the code. You're splitting on a comma... only there aren't any in the data... so your tempArray only ends up with a single element.
Also, mostly out of curiosity, is there a reason you're using the RegEx split rather than just the string split function? I mean if you were splitting off of something complex, I'd get it, but since it appears to be just off the lowly comma, regex seems like overkill to me.
-tg
Thanks for the reply. Actually i didn't use the TryParseExact method before and i don't know how.
can you give me the code for that?
Re: Facing issue in Date format (string was not recognized as a valid datetime in vb.
I'll do you one better. I'll throw you a fish - some cod.
-tg