I am reading a 3rd party text file using Text Field parser and then adding columns to a Table.
The program was working fine. The 3rd party must have changed the data type in one or more of the fields. So I now receive an input format error.
Unfortunately the way I currently have my code I can not determine which field is not correct. I am sure there is a better way to code this to provide message that lets me know which field is not being parsed? Can anyone give me a little help? Thank you
Here is the TextFieldParser code it creates all the columns as strings
Then here is how I am converting columns to doubles and timeCode:Using myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filepath) myReader.SetDelimiters(vbTab) Dim currentRow As String() currentRow = myReader.ReadFields() Dim colNameList As New List(Of String) Dim colName As String = String.Empty For i As Integer = 0 To currentRow.GetUpperBound(0) colName = currentRow(i) Dim suffix As Integer = 1 While colNameList.Contains(colName) colName = currentRow(i) & suffix.ToString suffix += 1 End While colNameList.Add(colName) Next For Each currentField As String In colNameList table.Columns.Add(currentField, GetType(System.String)) Next While Not myReader.EndOfData Try currentRow = myReader.ReadFields() table.Rows.Add(currentRow) Catch ex As Exception End Try End While End Using
Code:With dtsold .Columns.Add("NewListPrice", GetType(Double)) .Columns.Add("NewListDate", System.Type.GetType("System.DateTime")) .Columns.Add("NewDateSold", System.Type.GetType("System.DateTime")) .Columns.Add("NewAddress") .Columns.Add("DaysMarket", System.Type.GetType("System.Int16")) .Columns.Add("SoldPrice", GetType(Double)) Dim dtrow As DataRow For Each dtrow In dtsold.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 = StrConv(dtrow.Item(Form10.add_zip_txt.Text).ToString, VbStrConv.ProperCase) 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("NewListPrice") = Double.Parse(CStr(dtrow.Item(Form10.lp_txt.Text))) dtrow("NewListDate") = DateTime.Parse(CStr(dtrow.Item(Form10.ld_txt.Text))) dtrow("NewDateSold") = DateTime.Parse(CStr(dtrow.Item(Form10.DateSold_txt.Text))) dtrow("DaysMarket") = Integer.Parse(CStr(dtrow.Item(Form10.dom_txt.Text))) dtrow("SoldPrice") = Double.Parse(CStr(dtrow.Item(Form10.saleprice_txt.Text))) Next dtrow
Thank you




Reply With Quote