I need help with importing Excel file into SQL Server by using VB.NET. My coding runs fine but sometimes it occur an ex message that said convert date failed
Here the error. When I import it for the first time it work fine but after changing the primary key in excel and other things it error at date
This is the date in the Excel file. First time it works but second time it doesn't work. I write the date in Excel just like SQL Server date format like 2021-12-14 which is YYYY-MM-DD. I kinda confused about this for a month now... if i have 10 rows in Excel, sometime it occur the error about convert the date but still import the data into SQL Server but some of them not imported in sql
Try
OLEcon.Open()
With OLEcmd
.Connection = OLEcon
.CommandText = "select * from [Sheet1$]"
End With
OLEda.SelectCommand = OLEcmd
OLEda.Fill(OLEdt)
For Each r As DataRow In OLEdt.Rows
Dim intAge As Integer
intAge = Convert.ToInt32(r(2).ToString)
Dim dateLED As Date
dateLED = Convert.ToDateTime(r(11).ToString)
Dim dateDJ As Date
dateDJ = Convert.ToDateTime(r(12).ToString)
sql = "INSERT INTO MasterStaffListTry (EENo,Name,Age,AgeCategory,Gender,Ethnicity,Grade,Category,Department,Position,ReportingTo,LastEmploymentDate,DateJoin,LOCUM,Status) VALUES
('" & r(0).ToString & "','" & r(1).ToString & "','" & intAge & "','" & r(3).ToString & "','" & r(4).ToString & "',
'" & r(5).ToString & "' ,'" & r(6).ToString & "','" & r(7).ToString & "','" & r(8).ToString & "','" & r(9).ToString & "',
'" & r(10).ToString & "','" & dateLED.ToShortDateString & "','" & dateDJ.ToShortDateString & "','" & r(13).ToString & "' ,'" & r(14).ToString & "')"
resul = saveData(sql)
If resul Then
Timer1.Start()
End If
Next
This is my coding for import the Excel file. I think here the wrong part.