Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing null values from excel to sql

Status
Not open for further replies.

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
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
I got the same error.
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()
 
yes i did i tried that in my if statement as well. any other ideas?
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top