I'm coding my first VB.Net program.
The process is to read a .csv file and write the records to a SQL Server 2005 table.
I'm getting an error when a column is empty. I need to put a NULL in the column.
In the code below; Currentline(10) is either empty or has a value. If the value is presented, no error. If empty, then it throws an error.
This is the error message
"Incorrect syntax near ','."
I understand that it is because it is empty. I could check the field;
If CurrentLine(10).Equals("") Then
How do I set the String to NULL
End If
Using sr As New StreamReader("test.csv")
Do
'CurrentLine = sr.ReadLine
CurrentLine = sr.ReadLine.Split(",")
' First row of file is a header row
If CurrentLine(0) <> "Address" Then
strSQL = "Insert CHQ_Resources(Address, AnnualTargetHours, " & _
"BaseCurrency, CertificationCycle, CertificationHours, City, Country, " & _
"EmailAddress, EmployeeType, ExpenseApprover, FirstName, FirstPayroll, " & _
"HireDate, HourlyCostRate, HoursPerDay, Language, LastName, Location, NTUserId, OfficeNumber, " & _
"OfficeNumberExt, PayrollCycle, PayrollLagDays, PostalCode, Province, " & _
"ResourceText1, ResourceText2, ResourceText3, ResourceType, ReportsTo, " & _
"TerminationDate, TimeApprover, Title, UserDefinedResourceId, WebPassword, " & _
"ResourceText6, TimeZone) " & _
"Values ('" & CurrentLine(0) & "', " & CurrentLine(1) & ", '" & CurrentLine(2) & _
"', 'W', '0', '" & CurrentLine(3) & "', '" & CurrentLine(4) & "', '" & CurrentLine(5) & _
"', '" & CurrentLine(6) & "', '" & CurrentLine(23) & "', '" & CurrentLine(7) & "', '" & CurrentLine(8) & _
"', '" & CurrentLine(9) & "', " & CurrentLine(10) & ", '8', 'Eng','" & CurrentLine(11) & _
"', '" & CurrentLine(12) & "', '" & CurrentLine(13) & "', '" & CurrentLine(14) & _
"', '" & CurrentLine(15) & "', '" & CurrentLine(16) & "', '120', '" & CurrentLine(17) & _
"', '" & CurrentLine(18) & "', '" & CurrentLine(19) & "', '" & CurrentLine(20) & _
"', '" & CurrentLine(21) & "', '" & CurrentLine(22) & "', '" & CurrentLine(23) & _
"', '" & CurrentLine(24) & "', '" & CurrentLine(23) & "', '" & CurrentLine(25) & "', '" & CurrentLine(26) & _
"', 'compuware" & CurrentLine(26) & "', '" & CurrentLine(27) & "', '" & CurrentLine(28) & "')"
The process is to read a .csv file and write the records to a SQL Server 2005 table.
I'm getting an error when a column is empty. I need to put a NULL in the column.
In the code below; Currentline(10) is either empty or has a value. If the value is presented, no error. If empty, then it throws an error.
This is the error message
"Incorrect syntax near ','."
I understand that it is because it is empty. I could check the field;
If CurrentLine(10).Equals("") Then
How do I set the String to NULL
End If
Using sr As New StreamReader("test.csv")
Do
'CurrentLine = sr.ReadLine
CurrentLine = sr.ReadLine.Split(",")
' First row of file is a header row
If CurrentLine(0) <> "Address" Then
strSQL = "Insert CHQ_Resources(Address, AnnualTargetHours, " & _
"BaseCurrency, CertificationCycle, CertificationHours, City, Country, " & _
"EmailAddress, EmployeeType, ExpenseApprover, FirstName, FirstPayroll, " & _
"HireDate, HourlyCostRate, HoursPerDay, Language, LastName, Location, NTUserId, OfficeNumber, " & _
"OfficeNumberExt, PayrollCycle, PayrollLagDays, PostalCode, Province, " & _
"ResourceText1, ResourceText2, ResourceText3, ResourceType, ReportsTo, " & _
"TerminationDate, TimeApprover, Title, UserDefinedResourceId, WebPassword, " & _
"ResourceText6, TimeZone) " & _
"Values ('" & CurrentLine(0) & "', " & CurrentLine(1) & ", '" & CurrentLine(2) & _
"', 'W', '0', '" & CurrentLine(3) & "', '" & CurrentLine(4) & "', '" & CurrentLine(5) & _
"', '" & CurrentLine(6) & "', '" & CurrentLine(23) & "', '" & CurrentLine(7) & "', '" & CurrentLine(8) & _
"', '" & CurrentLine(9) & "', " & CurrentLine(10) & ", '8', 'Eng','" & CurrentLine(11) & _
"', '" & CurrentLine(12) & "', '" & CurrentLine(13) & "', '" & CurrentLine(14) & _
"', '" & CurrentLine(15) & "', '" & CurrentLine(16) & "', '120', '" & CurrentLine(17) & _
"', '" & CurrentLine(18) & "', '" & CurrentLine(19) & "', '" & CurrentLine(20) & _
"', '" & CurrentLine(21) & "', '" & CurrentLine(22) & "', '" & CurrentLine(23) & _
"', '" & CurrentLine(24) & "', '" & CurrentLine(23) & "', '" & CurrentLine(25) & "', '" & CurrentLine(26) & _
"', 'compuware" & CurrentLine(26) & "', '" & CurrentLine(27) & "', '" & CurrentLine(28) & "')"