import xls data into datagridview on form

I'm attempting to allow users to either manually enter data or import an XLS file to fill a datagridview. I'm having dificulty getting the formatting to correct fill the DGV.

the project file and sample files are at this link 
Judson HallSystems AdminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Craig WagnerSoftware ArchitectCommented:
You'll need to make some adjustments for the following code to work.

Declare a private member on the class to hold the name of the Excel file.

private XLSPath as string

Add a first row to the spreadsheet for column names.

Make sure the DataGrid columns map to the names you give the columns in Excel.
        Dim connectionStringTemplate As String = _
    "Provider=Microsoft.ACE.OLEDB.12.0;" + _
    "Data Source={0};" + _
    "Extended Properties=""Excel 12.0;HDR=Yes"""
        Dim connectionString As String = String.Format(connectionStringTemplate, XLSPath)
        Dim sqlSelect As String = "SELECT * FROM [Sheet1$];"
        ' Load the Excel worksheet into a DataTable
        Dim workbook As DataSet = New DataSet()
        Dim excelAdapter As System.Data.Common.DataAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlSelect, connectionString)
        Dim worksheet As DataTable = workbook.Tables(0)
        DataGridView1.DataSource = worksheet

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Judson HallSystems AdminAuthor Commented:
I'm getting an error at the call for "XLSPath" (line 6) and at line 13
Craig WagnerSoftware ArchitectCommented:
Did you declare the variable?
Judson HallSystems AdminAuthor Commented:
I thought I did in the snippet right above this section.
'let user select xls file to process
    Private Sub ImportXLS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportXLS.Click
        If OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim XLSPath As String = OpenFileDialog.FileName
        End If
    End Sub

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.