I have looked but cannot find a solution for my problem. I am importing data from a CSV file with the following code:
Using connection As New SqlClient.SqlConnection(SqlconnectionString)
Dim cmd As New SqlClient.SqlCommand(strsql, connection)
With cmd.Parameters
.Add("@PLAASNOMMER", SqlDbType.VarChar, 50, "F1")
.Add("@PLAASNAAM", SqlDbType.VarChar, 50, "F2")
.Add("@BLOKNOMMER", SqlDbType.VarChar, 50, "F3")
.Add("@AREA", SqlDbType.VarChar, 50, "F4")
.Add("@KULT", SqlDbType.VarChar, 50, "F7")
.Add("@WINGERDKLAS", SqlDbType.VarChar, 50, "F8")
.Add("@ANALISEDATUM", SqlDbType.Date).Value = Date.ParseExact("F9", "dd/MM/yyyy", DBNull.Value)
.Add("@SUIKERWAARDE", SqlDbType.Decimal, 50, "F10")
.Add("@PHWAARDE", SqlDbType.Decimal, 50, "F11")
.Add("@SUURWAARDE", SqlDbType.Decimal, 50, "F12")
End With
Dim adapter As New SqlClient.SqlDataAdapter()
adapter.InsertCommand = cmd
Dim iRowsInserted As Int32 = adapter.Update(dt)
I have "empty" dates in the CSV file as "/ / ". I am using the code (line 10) :
.Add("@ANALISEDATUM", SqlDbType.Date).Value = Date.ParseExact("F9", "dd/MM/yyyy", DBNull.Value)
However I am getting an error
Unable to cast object of type 'System.DBNull' to type 'System.IFormatProvider'.
What am I missing?
My CSV date looks like this:
plaasno,name1,blokno,name2,analise_dt,name3,kult,wingklas,datum,suiker,ph,suur,deurryppnt,geur
"11332","MONOTONKA","1-MON","KLEINPLASIE","20090115","","PIN","KULT"," / /",0.00,0.00,0.00,0.00,0.00
"11332","MONOTONKA","10-MON","KLEINPLASIE","20090115","","COL","BULK"," / /",0.00,0.00,0.00,0.00,0.00
"11332","MONOTONKA","10-MON","VREDENDAL","20180228","","COL","BULK","28/02/18",23.50,3.08,10.42,0.00,0.00
"11332","MONOTONKA","12-MON","KLEINPLASIE","20090115","","CHB","BULK"," / /",0.00,0.00,0.00,0.00,0.00
"11332","MONOTONKA","12-MON","VREDENDAL","20180214","","CHB","BULK","14/02/18",18.70,3.09,11.78,0.00,0.00
"11332","MONOTONKA","13-MON","KLEINPLASIE","20090115","","CHB","BULK"," / /",0.00,0.00,0.00,0.00,0.00
"11332","MONOTONKA","13-MON","VREDENDAL","20180214","","CHB","BULK","14/02/18",18.30,3.03,13.28,0.00,0.00
"F1"is the literal string "F1". It is not the first field of the CSV file. You need to do some more programming the get the fields.