Note that there are some explanatory texts on larger screens.

plurals
  1. POImport Excel sheet into datagridview - does workbook need to be closed to use OleDB?
    primarykey
    data
    text
    <p>I'm trying to import an Excel sheet into datagridview, but I'm getting hit with an error: "database or object is read-only." However, the workbook I'm referencing does not have the read-only attribute applied. That being said, the workbook I'm connecting to is already open if my application is running, so I suspect this is the reason I'm getting hit with this error. The workbook is open, thus appears to the system as read-only when trying to fill the dataset.</p> <p>Am I right in this assumption? Is their a way to import an Excel sheet into datagridview using the OleDB connection if the workbook I'm connecting to is open? If not, is there any other way to populate this datagridview without having to do a massive loop through my sheet? My code is as follows:</p> <pre><code> Try 'connect to Excel data source and set gridview equal to dataset (entire sheet should be visible in gridview) Dim MyConnection As System.Data.OleDb.OleDbConnection Dim MyCommand As System.Data.OleDb.OleDbDataAdapter Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;" &amp; "Data Source=" &amp; StatVar.workbookName &amp; ";Extended Properties=""Excel 12.0;HDR=YES;Readonly=False"";" 'may need to use different MS provider and lower OLEDB for .xls files (Microsoft.Jet.OLEDB4.0...Excel 8.0) kind of sketchy though MyConnection = New System.Data.OleDb.OleDbConnection(connstring) 'create a new data connection to Excel data source MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Budget$]", MyConnection) 'query the sheet - select all data MyCommand.TableMappings.Add("Table", "Table") 'map data selection as table StatVar.DtSet1 = New System.Data.DataSet 'create new data set MyCommand.Fill(StatVar.DtSet1) 'fill data set with table Form14.DataGridView1.DataSource = StatVar.DtSet1.Tables(0) 'populate gridview with data set table MyConnection.Close() Form14.ShowDialog() Catch exc As Exception MessageBox.Show("There was a problem loading this database. Please contact an administrator if the problem continues." &amp; vbNewLine &amp; vbNewLine &amp; "Error: " &amp; exc.Message) End Try </code></pre> <p>I'm getting hit with the error here:</p> <pre><code>MyCommand.Fill(StatVar.DtSet1) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload