|
-
Oct 17th, 2012, 03:02 PM
#1
Thread Starter
Frenzied Member
[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
-
Oct 17th, 2012, 03:15 PM
#2
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
 
-
Oct 17th, 2012, 04:58 PM
#3
Thread Starter
Frenzied Member
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
-
Oct 17th, 2012, 05:30 PM
#4
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
-
Oct 17th, 2012, 06:01 PM
#5
Thread Starter
Frenzied Member
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
-
Oct 17th, 2012, 08:58 PM
#6
Thread Starter
Frenzied Member
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
-
Oct 18th, 2012, 10:26 AM
#7
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
 
-
Oct 18th, 2012, 11:18 AM
#8
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
-
Oct 18th, 2012, 01:55 PM
#9
Thread Starter
Frenzied Member
Re: DateTime.TryParse
Thanks for your help guys it is appreciated
-
Oct 18th, 2012, 02:05 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|