Dear Sir ,
I have an error `Conversion from string "1971--10--29" to type 'Date' is not valid.` when import datfile in VB.NET
How can I ignore for error in database it because that's indeed the problem from the database
Please Guide me
Thanks
Sample File dat
1037 2020-01-02 07:57:38 1 0 0 0
1002 2020-01-02 07:57:42 1 0 0 0
1033 2020-01-02 07:57:48 1 0 0 0
1039 2020-01-02 07:58:02 1 0 0 0
1033 2020-01-02 17:35:43 1 0 0 0
4036 2020-01-02 17:36:12 1 0 0 0
3014 2020-01-02 17:37:21 1 0 0 0
1037 2020-01-02 17:37:35 1 0 0 0
4036 2020-01-02 17:38:03 1 0 0 0
4036 2020-01-02 17:38:29 1 0 0 0
1035 1971--10--29 -23:-59:-50 1 0 0 0
1033 2020-01-02 17:39:37 1 0 0 0
Code in vb.net
Imports System.ComponentModel
Imports System.IO
Public Class Form1
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
OpenFileDialog1.InitialDirectory = "D:\Files"
OpenFileDialog1.Filter = "Dat files(*.Dat)|*.Dat"
OpenFileDialog1.RestoreDirectory = True
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim datfiles As List(Of datfiles) = New List(Of datfiles)()
Dim lines As String() = File.ReadAllLines(OpenFileDialog1.FileName)
For i As Integer = 0 To lines.Length - 1
If Not String.IsNullOrEmpty(lines(i).Trim()) Then
Dim data As String() = lines(i).Trim().Split(CType(vbTab, Char()))
datfiles.Add(New datfiles With {
.ID = Convert.ToInt32(data(0)),
.DATE = CDate(data(1).ToString().Split(" "c)(0)),
.TIME = data(1).ToString().Split(" "c)(1),
.FP = data(2),
.INOUT = If(data(3).ToString() = "0", "IN", "OUT"),
.OTHERS1 = data(4),
.OTHERS2 = data(5),
.STATUS = If(data(2).ToString() = "1", "PRESENT", "")
})
End If
Next
Dim dtStart As DateTime = startDate.Value.Date
Dim dtEnd As DateTime = endDate.Value.Date
datfiles = datfiles.Where(Function(x) x.DATE >= dtStart AndAlso x.DATE <= dtEnd).ToList()
Dim All_IDs As List(Of Integer) = (
From df In datfiles
Group By the_ID = df.ID
Into Group
Select the_ID
).ToList
Dim All_Dates As New List(Of Date)
Dim day As Date = dtStart
While day <= dtEnd
All_Dates.Add(day)
day = day.AddDays(1)
End While
With datfiles
All_Dates.ForEach(Sub(the_Date)
All_IDs.ForEach(Sub(the_ID)
If .Where(Function(x) x.DATE = the_Date AndAlso x.ID = the_ID AndAlso x.INOUT = "IN").Count = 0 Then
.Add(New datfiles With {.ID = the_ID, .[DATE] = CDate(the_Date.ToString("yyyy-MM-dd")), .INOUT = "IN"})
End If
If .Where(Function(x) x.DATE = the_Date AndAlso x.ID = the_ID AndAlso x.INOUT = "OUT").Count = 0 Then
.Add(New datfiles With {.ID = the_ID, .[DATE] = CDate(the_Date.ToString("yyyy-MM-dd")), .INOUT = "OUT"})
End If
End Sub)
End Sub)
End With
DataGridView1.DataSource = datfiles.OrderBy(Function(x) x.DATE).ThenBy(Function(x) x.ID).ToList()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
startDate.Value = CDate("2020-01-02")
endDate.Value = CDate("2020-01-03")
End Sub
End Class
Public Class datfiles
Public Property ID As Integer
Public Property [DATE] As DateTime
Public Property TIME As String
Public Property FP As String
<DisplayName("IN/OUT")>
Public Property INOUT As String
Public Property OTHERS1 As String
Public Property OTHERS2 As String
Public Property STATUS As String
End Class