Operation must use an updateable query when updating excel sheet Operation must use an updateable query when updating excel sheet asp.net asp.net

Operation must use an updateable query when updating excel sheet


Remove the IMEX=2 (or IMEX=1) from the connection string and it will work. I have tested this crazy solution several times and removing the IMEX for some strange reason seems to do the trick (at least for xlsx files).

The following code works:

    static void Main(string[] args)    {        string connectionString  = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "d:\\temp\\customers.xlsx" + ";Extended Properties=\"Excel 12.0;ReadOnly=False;HDR=Yes;\"";        string selectString = "INSERT INTO [Customers$](Id,Company) VALUES('12345', 'Acme Inc')";        OleDbConnection con = new OleDbConnection(connectionString);        OleDbCommand cmd = new OleDbCommand(selectString, con);        try        {            con.Open();            cmd.ExecuteNonQuery();            Console.WriteLine("Success");        }        catch (Exception ex)        {            Console.WriteLine(ex.Message);        }        finally        {            con.Dispose();        }        Console.ReadLine();    }}

Thanks to RobertNet from social.msdn.microsoft.com


I used the solution provided above and removed the IMEX=2 or IMEX=1 string from the connection string. But this was not enough. In my case, the solution needed an additional work around.

But in my data base I actually needed the IMEX - because my column had mixed data types, some double values some string values. When I remove IMEX =1, I get the a runtime exception "Unable to cast object of type" because it automatically selects the column data type based on the most popular value in the column and then fails to cast the values which are not of the selected type.

I worked around this issue by changing my double and int values to string values (added a ' in the beginning of the cell value manually in excel) and removed the IMEX from the connection string. and then this solved the issue.


In my case,I changed ConnectionString and then this solved the issue.
I removed ReadOnly=False;HDR=Yes; parametes in connectionString

string _connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;\";", pathToExcelFile);