Results 1 to 4 of 4

Thread: Facing issue in Date format (string was not recognized as a valid datetime in vb.net)

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    6

    Exclamation 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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    6

    Exclamation Re: Facing issue in Date format (string was not recognized as a valid datetime in vb.

    Quote Originally Posted by techgnome View Post
    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?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width