I am importing an Excel spreadsheet, turning it into a dataset and inserting the results into SQL Server (only 100 rows).
I am having an issue with one of the date columns in Excel, which can have a value or not. The "F" column is the one that may be blank and this is the error I get: Conversion from type 'DBNull' to type 'String' is not valid.
How can I add a check to the Dim F As Date = CDate(row(5)) to handle a null?
VB Code:
' The use of letters instead of ' more descriptive variables is ' strictly for this sample peice ' of code. Public Sub PushDS2SQL() Try Dim row As DataRow For Each row In ExcelSlurp.Tables(0).Rows Dim A As String = CStr(row(0)) Dim B As String = CStr(row(1)) Dim C As String = CStr(row(2)) Dim D As Integer = CInt(row(3)) Dim E As Date = CDate(row(4)) Dim F As Date = CDate(row(5)) AddEligEmp(A, B, C, D, E, F) Next row MessageBox.Show( _ "Eligible Employees Added", _ "Success", _ MessageBoxButtons.OK) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub




Reply With Quote