Results 1 to 10 of 10

Thread: [RESOLVED] DateTime.TryParse

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Resolved [RESOLVED] DateTime.TryParse

    I need a little help implementing a solution when my program is reading a 3rd party database that might be missing a date null or empty or not be in a vlaid datetime format that can be parsed.

    My program reads a 3rd party text file database using text field parser then I create a new column and fill it with data by parsing in the following way:

    With dtlist
    .Columns.Add("NewListPrice", GetType(Double))
    .Columns.Add("NewListDate", GetType(DateTime))
    .Columns.Add("NewAddress")
    .Columns.Add("DaysMarket", GetType(Int16))
    Dim dtrow As DataRow
    For Each dtrow In dtlist.Rows()
    Dim StrNum As String = dtrow.Item(Form10.add_Num_txt.Text).ToString
    Dim StrName As String = StrConv(dtrow.Item(Form10.add_Str_txt.Text).ToString, VbStrConv.ProperCase)
    Dim UnitNum As String = dtrow.Item(Form10.add_unit_txt.Text).ToString
    Dim St As String = dtrow.Item(Form10.add_state_txt.Text).ToString
    Dim Zip As String = dtrow.Item(Form10.add_zip_txt.Text).ToString
    Dim City As String = StrConv(dtrow.Item(Form10.add_city_txt.Text).ToString, VbStrConv.ProperCase)
    Dim Words() As String = Split(City, "(")
    Dim revCity As String = Words(0)
    dtrow("NewAddress") = StrNum + " " + StrName + " " + UnitNum + " " + City + "," + " " + St + " " + Zip
    dtrow("NewListDate") = DateTime.Parse(CStr(dtrow.Item(Form10.ld_txt.Text)))
    dtrow("DaysMarket") = Integer.Parse(CStr(dtrow.Item(Form10.dom_txt.Text)))
    dtrow("NewListPrice") = Double.Parse(CStr(dtrow.Item(Form10.lp_txt.Text)))
    Next dtrow



    I need a solution for when a field in the 3rd party textfile is empty. I recently encountered a problem when a DateTime field was empty. I would like the program to make the user aware the field was empty but also continue to run. I am not sure what they best way to do this would be. I was reading about tryparse, still a bit confused on implementing it though and not sure if that the best way for my over goal.

    Ideally it might be nice to let the user know one of the fields was empty and insert a 01/01/0000 or something similiar date so the program can continue but the data would be obvious to the user is in correct.

    does anyone of any good practical ideas and possible solutions?

    Thank

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: DateTime.TryParse

    TryParse might not work, in this case. TryParse is good for turning strings into dates, if that is possible, and telling you when it is not possible. That would be ok if your empty fields appear to just be "", which may be the case since you said this was a text file database. If the value is Null, I think TryParse won't work. Therefore, that's the first thing to figure out. If you have empty strings, what you would be doing would look something like this:

    Code:
    If Date.TryParse(dtrow.Item(CSTR(Form10.ld_txt.Text).ToString,dtrow("NewListDate")) Then
     'It was a date, and is now in the right field in dtrow.
    Else
     'It was not a date, so do what you want.
    End If
    This may not work. That field in the datarow would be type object. TryParse might require a Date type, so you might have to create a variable of date type to pass as the second argument, then if TryParse returns True you can put that temporary into the datarow field.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: DateTime.TryParse

    Thanks Shaggy,
    I am a bit confused still

    Dim baddate As Date = Date.FromOADate(1 / 1 / 1950)
    Dim datevalue As Date

    If Date.TryParse(CStr(dtrow.Item(Form10.ld_txt.Text)), datevalue) Then
    'It was a date, and is now in the right field in dtrow.
    Else
    'It was not a date, so do what you want.
    MsgBox("One or more Comparables is Missing a List Date")
    dtrow("NewListDate") = baddate
    End If

    This causes all records to be blank/empty and the one bad record to have the date 12/30/1899

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

    Re: DateTime.TryParse

    well first, you didn't convert the date in to the data row... that's why they are blank. You parsed it into the datevalue variable.

    your baddate is wrong because you converted 1/1/1950 into a date (that's 1 divided by 1 divided by 1950) .... which resulted in a value too small to represent right... if you want tpo fix baddate to a specific date you should have done it like this:

    Code:
    Dim baddate As Date = New Date(1950, 1, 1)
    -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??? *

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: DateTime.TryParse

    Thank you.. Techgnome

    How do i parse it into the NewListDate ?

    When I tried

    If Date.TryParse(CStr(dtrow.Item(Form10.ld_txt.Text)), (dtrow("NewListDate"))) Then
    I get an error implicit conversion form object to date not allowed

    Then I tried

    CDate((dtrow.Item("NewListDate"))))

    but end up withthe same problem as i initially had when i run the program

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: DateTime.TryParse

    I think i have it, it at least appears to working correctly

    If Date.TryParse(CStr(dtrow.Item(Form10.ld_txt.Text)), datevalue) Then
    dtrow("NewListDate") = datevalue
    'It was a date, and is now in the right field in dtrow.
    Else
    'It was not a date, so do what you want.
    MsgBox("One or more Comparables is Missing a List Date")
    dtrow("NewListDate") = baddate
    End If

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: DateTime.TryParse

    I was afraid that you would have to use a date variable rather than a datarow field as the second argument to TryParse, and that is clearly the case. It looks like you have it.
    My usual boring signature: Nothing

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

    Re: DateTime.TryParse

    because the parameter is byref, and dtrow() returns object (I think it may do an implicit conversion at the last second) ... this doesn't surprise me.... I can't remember if a DataRow contains a .Value member... if it did, I'd try passing that explicitly (right now it's using the default property) ... and it *might* work... but I think it depends on how it's typed.

    -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??? *

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: DateTime.TryParse

    Thanks for your help guys it is appreciated

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [RESOLVED] DateTime.TryParse

    I think I should have figured out for myself that you would HAVE to pass in a variable of type Date. I knew that a datarow field was type Object, and it would be utterly unreasonable to expect that TryParse would take such a thing and implicitly convert it into something that would accept the right type, then cast it back to Object, which is effectively what I was asking it to do. After all, if TryParse was willing to accept an argument of type object, you would be able to pass it ANYTHING, such as a Form, or Integer, or even a string. It certainly wouldn't be able to put a date into any of those in any meaningful way. Technically, it COULD do something if the object was a string, but what it could do would so thoroughly remove the purpose for the method that it would be unreasonable.
    My usual boring signature: Nothing

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