4
Answers

Error: external table is not in the expected format

Sathya Narayan

Sathya Narayan

8y
25.7k
1
Hi,

I am getting an error when fetching the excel sheet to datagridview.This is the error i am getting external table is not in the expected format in vb.net.So any one find where exactly the error.The values values are not Binding to dataset.

This is my code

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim openbox As New OpenFileDialog
            openbox.Filter = "Excel Workbook (*.xls) |*.xls;*.xlsx"
            openbox.ShowDialog()


            Dim fname As String = openbox.FileName
            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=" & fname & "; Extended Properties=Excel 8.0")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter _
                ("select * from [Sheet1$]", MyConnection)
            Dim workbook As DataSet = New DataSet()
            MyCommand.TableMappings.Add("Table", "testtable")
            DtSet = New System.Data.DataSet
            MyCommand.Fill(DtSet, "testtable")
            DataGridView1.DataSource = DtSet.Tables(0)
            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
Thanks & Regards,

sathya
 
Answers (4)