Importing a spreadsheet, but having trouble.

Posted by Chris Phelps on Stack Overflow See other posts from Stack Overflow or by Chris Phelps
Published on 2010-04-09T19:31:27Z Indexed on 2010/04/09 19:33 UTC
Read the original article Hit count: 212

Filed under:

I have a form that allows a user to import a spreadsheet. This spreadsheet is generally static when it comes to column headers, but now the users want to be able to include an optional column (called Notes). My code crashes when I try to read the column from the spreadsheet if it doesn't exist.

        Dim objCommand As New OleDbCommand()
        objCommand = ExcelConnection() 'function that opens spreadsheet and returns objCommand

        Dim reader As OleDbDataReader
        reader = objCommand.ExecuteReader()

        While reader.Read()
            Dim Employee As String = Convert.ToString(reader("User"))
            Dim SerialNUM As String = Convert.ToString(reader("serialno"))
            **Dim Notes As String = Convert.ToString(reader("notes"))**

If the spreadsheet contains a Notes column, all goes well. If not, crash. How can I check to see if the Notes column exists in the spreadsheet to avoid the crash?

© Stack Overflow or respective owner

Related posts about ASP.NET