Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I'm trying to update an excel file. I used OLEDB connection for read data from excel and now i want to use it for update the data.

I can import data from excel to datagridview. So if there is anyway to update all data from datagridview to excel, it would be great. I'm open for any suggestion.

Here is my code :

C#
OleDbConnection baglan = new OleDbConnection();
                            baglan.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + yeniDosya + "; Extended Properties = 'Excel 8.0;IMEX=1;'";
                            baglan.Open();
                            string sit = sheetadi;
                            sit = sit.TrimEnd('\'');
                            sit = sit.TrimStart('\'');
                            string shtsrg = sit + "K" + i + ":K" + i;
                            
                            string excel = String.Format("UPDATE {0} SET F1=" + dataGridView1.Rows[i].Cells[9].Value.ToString() + "", shtsrg);
                            MessageBox.Show(excel);
                            OleDbCommand cmd = new OleDbCommand(excel, baglan);
                            cmd.ExecuteNonQuery();



Let me explain simply :

yenidosya : is excel files name
sheetadi : is sheets name ( i am taking it from somewhere else )
In sheetadi i have ' at start and end. so i trim it
i is my column number and it can change parametric
with shtsrg i want to merge sheetname and column number ( it's also my table name )
I used excel string for make table name parametric

I'm trying to use this method :

How to update single cell of excel sheet using oledb connection, | The ASP.NET Forums[^]

But it gives me an error :
Syntax error in UPDATE statement 


Do you have any suggestion about where is Syntax error , or which block has problem ?

What I have tried:

How to update single cell of excel sheet using oledb connection, | The ASP.NET Forums[^]
Posted
Updated 13-Feb-17 8:11am
v2
Comments
ZurdoDev 13-Feb-17 14:42pm    
Just look at the sql and find it. Not hard to do.

1 solution

Nope. And we can't - we have no idea what your UPDATE command looks like, because you are building it (badly, but I'll come to that) from parts which are not available to us: A DataGridView cell content, and a variable.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Comments
kozmikadam 13-Feb-17 14:27pm    
Hello ,
I'm not a pro-coder. So i guess it's normal to make this type faults and learning : what i shouldn't do it and why. :)

I'm using breakpoints also.
I can say excel string is : UPDATE 30187$K6:K6 SET F1=OK
But when i put this string in to directly excel string ( without any parametric content ) it's gives same error.

And when i tried just like on the link, i get same error.

Or do you have any other suggestion for update excel's some cell's value from datagridview ?
OriginalGriff 13-Feb-17 14:37pm    
Assuming that "30187" is your sheet name, try quoting the value, and escaping the address:
UPDATE [30187$K6:K6] SET F1='OK'
kozmikadam 13-Feb-17 14:38pm    
i tried this also :) still got error.
Michael_Davies 13-Feb-17 14:57pm    
Normally sheet names are contained in single quotes try UPDATE '30187'$K6:K6 SET F1=OK
kozmikadam 13-Feb-17 15:04pm    
hi, it gives syntax error still.

But i restart my computer, and now it didn't give any error for : UPDATE [30187$K6:K6] SET F1='OK' but now program freezes. And it isn't in any loop.

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