Results 1 to 25 of 25

Thread: (Resolved) Conversion from type 'DBNull' to type 'String' is not valid

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    (Resolved) Conversion from type 'DBNull' to type 'String' is not valid

    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:
    1. ' The use of letters instead of
    2.     ' more descriptive variables is
    3.     ' strictly for this sample peice
    4.     ' of code.
    5.     Public Sub PushDS2SQL()
    6.         Try
    7.             Dim row As DataRow
    8.             For Each row In ExcelSlurp.Tables(0).Rows
    9.                 Dim A As String = CStr(row(0))
    10.                 Dim B As String = CStr(row(1))
    11.                 Dim C As String = CStr(row(2))
    12.                 Dim D As Integer = CInt(row(3))
    13.                 Dim E As Date = CDate(row(4))
    14.                 Dim F As Date = CDate(row(5))
    15.                 AddEligEmp(A, B, C, D, E, F)
    16.             Next row
    17.             MessageBox.Show( _
    18.                 "Eligible Employees Added", _
    19.                 "Success", _
    20.                 MessageBoxButtons.OK)
    21.         Catch ex As Exception
    22.             MessageBox.Show(ex.Message)
    23.         End Try
    24.     End Sub
    Last edited by FastEddie; May 1st, 2009 at 10:10 AM. Reason: Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width