Click here to Skip to main content
16,017,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to update an excel file with using c# oledb way.

I have excel files more than one. One of then hasn't any problem and i can update like what i want.
But other excel files i have some missmatch column.
I want to update cell like a string but it's type is number ( on Excel ), So i'm getting mismatch error for this.

I look at the forums and saw IMEX=1 can solve the problem.
But when i add IMEX=1 i got this error :

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Operation must use an updateable query.


Here is my code block :

string excel = String.Format("UPDATE {0} SET F1='{1}'", shtsrg,exres);
string exceltwo = String.Format("UPDATE {0} SET F1='{1}'", shtsrgtwo, exrestwo);

OleDbConnection baglans = new OleDbConnection();
    baglans.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + yeniDosya + "; Extended Properties = 'Excel 8.0; HDR=NO'";
    baglans.Open();
OleDbCommand cmds = new OleDbCommand(excel, baglans);
cmds.ExecuteNonQuery();
baglans.Close();

OleDbConnection baglanstwo= new OleDbConnection();
baglanstwo.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + yeniDosya + "; Extended Properties = 'Excel 8.0; HDR=NO; IMEX=1'";
baglanstwo.Open();

OleDbCommand cmdstwo = new OleDbCommand(exceltwo, baglanstwo);

cmdstwo.ExecuteNonQuery();

baglanstwo.Close();


I used 2 connection for just i want to see where is the problem. I'm updating 2 columns. With 1. connection i haven't any problem. I have a problem with second one.

Is there anyway for change excel's cell data type without Imex ? Or anything solve Updateable query error ?

What I have tried:

Imex=1 is not solving my problem and gives another one.
Posted
Updated 16-Feb-17 2:16am

1 solution

You should read the item(s) first with a SELECT statement, and check exactly what type each variable returns. You can then update it with the correct value.
 
Share this answer
 
Comments
kozmikadam 16-Feb-17 8:18am    
I'm already know their type and use select on the top of this code for the read.
But i need to put a string value in to cell which type is integer. So i got missmatch type error for this.
Richard MacCutchan 16-Feb-17 9:59am    
So, don't try putting a string into a field that only accepts an integer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900