3

i am trying to read in some data from an excel:

If (FileUpload1.PostedFile.ContentType = "application/vnd.ms-excel") Then
                Dim filename As String = Path.GetFileName(FileUpload1.FileName)
                'Session("userid") & "-" & Date.Now()
                filepath = "\excel\" & Session("userid") & "_" & Now.Date().ToString("Mdy") & "_" & filename
                FileUpload1.SaveAs(Server.MapPath("~/") & filepath)
                ReadExcel(filepath)

            Else
                StatusLabel.Text = "Only Excel file types are accepted"
            End If

everything seems to be fine, except when one of the columns sometimes reads in as NULL. seems like that happens if it's of a different type. i can't figure out what it is. someone help me please....

Sub ReadExcel(ByVal filepath As String)
    Dim MyConnection As System.Data.OleDb.OleDbConnection
    Dim DtSet As System.Data.DataSet
    Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
    MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & Server.MapPath("~/") & filepath & "';Extended Properties=Excel 8.0;")

    DtSet = New System.Data.DataSet
    Try
        MyCommand.Fill(DtSet)
        'gwResults.DataSource = DtSet.Tables(0)
        LoopSources(DtSet)
        'gwResults.DataBind()
        MyConnection.Close()
    Catch ex As Exception
        StatusLabel.Text = "Import Excel status: The file could not be loaded. The following error occured: <br>" + ex.Message
    End Try

End Sub
0

2 Answers 2

5

You need to include Extended Properties="Excel 8.0;IMEX=1;" in your connection string. Excel tries to determine the data type of your columns by reading the first few rows. Once it thinks it knows the data type, anything that doesn't conform to that is coerced to NULL. Very annoying. IMEX=1 tells it to read your data as intermixed and should resolve your problem. If you have a header row, you should also consider including HDR=YES. See this link: http://www.connectionstrings.com/excel for more info.

Sign up to request clarification or add additional context in comments.

3 Comments

new connection string: MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & Server.MapPath("~/") & filepath & "';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'") now works! thank you!!!
No problem. I feel you pain as I had to figure this one out myself some time ago. But, if you really want to thank me, you could always mark your question as answered :) Happy coding!
I have been battling with DBNull all morning. Thanks !
1

Probably the type of column is determined incorrectly. By default Jet provider reads 8 first rows to determine the type of column. And if these 8 lines contain something wrong you'll get a problem.

This can be changed by setting TypeGuessRows setting in registry.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.