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