gtjr921
Programmer
- Aug 30, 2006
- 115
I have a page that imports data from an excel file
it works great if none of the columns in excel are null, but if one column is null is throws the specified cast is not valid error.
I tried doing
I got the same error.
Here is the code for my page
it works great if none of the columns in excel are null, but if one column is null is throws the specified cast is not valid error.
I tried doing
Code:
If Not (rdr.GetString(1)= DbNull.Value.ToString()) then
cmd.Parameters.AddWithValue("@Homephone", rdr.GetString(13)) Else
cmd.Parameters.AddWithValue("@Homephone", DbNull.Value) End If
and also tried
If Not (rdr.GetString(1)= DbNull.Value.ToString()) then
cmd.Parameters.AddWithValue("@Homephone", rdr.GetString(13)) Else
cmd.Parameters.AddWithValue("@Homephone", "") End If
Here is the code for my page
Code:
Dim rdr As Data.OleDb.OleDbDataReader = cmdXLS.ExecuteReader
Dim cmd As New Data.SqlClient.SqlCommand("storedproc", Conn)
Do While rdr.Read()
If rdr.GetString(0) <> "First Name" Then
cmd.CommandType = Data.CommandType.StoredProcedure
'cmd.Parameters.AddWithValue("@Telephone", Replace(rdr.GetString(0), "-", ""))
cmd.Parameters.AddWithValue("@FirstName", rdr.GetString(0))
cmd.Parameters.AddWithValue("@LastName", rdr.GetString(1))
cmd.Parameters.AddWithValue("@Title", rdr.GetString(2))
cmd.Parameters.AddWithValue("@Gender", rdr.GetString(3))
cmd.Parameters.AddWithValue("@OfficeName", rdr.GetString(4))
Conn.Open()
cmd.ExecuteNonQuery()
Conn.Close()
cmd.Parameters.Clear()
End If
Loop
rdr.Close()
connXLS.Close()