Syntax error in UPDATE statement.
-
Friday, August 17, 2012 8:21 PM
I am trying to update information in specific excel rows/colums, and I keep gettingSyntax error in UPDATE statement.
here is my code:
protected void Button2_Click(object sender, EventArgs e) { using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\AIRSIGNDMC\wwwroot\Contract.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=NO"""); string sql = string.Format("UPDATE sheet.Cells[6, B] = VALUES ('{0}')", TextBox1.Text); using (OleDbCommand cmd = new OleDbCommand(sql, conn)) { try { conn.Open(); cmd.ExecuteNonQuery(); } finally { conn.Close(); } conn.Close(); } } } } }
Thank you!
All Replies
-
Saturday, August 18, 2012 6:56 AMYou are mixing VBA and SQL grammar. Make up your mind. Either use VBA or use SQL, not both.
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
Sunday, August 19, 2012 2:04 PM
Please review syntax for an SQL Update statement here http://www.w3schools.com/sql/sql_update.asp then look at your update statement which indicates a syntax error because a) you can not have sheet.Chell[6,B] = VALUES {'{0}') but instead need to have the update statement conform as per the link above and note the comment about not having a Where clause in the SQL update statement you will update all rows. Also check out the following Code Project article on OleDb and Excel http://www.codeproject.com/Articles/37055/Working-with-MS-Excel-xls-xlsx-Using-MDAC-and-Oled#dml in regards to updating data.
KSG
- Marked As Answer by Chester HongMicrosoft Contingent Staff, Moderator Tuesday, September 04, 2012 9:19 AM
-
Monday, August 20, 2012 2:46 PMI would like to use sql, where is my VBA grammar? I don't really know what you are talking about...
-
Monday, August 20, 2012 2:57 PM
UPDATE [w1] SET secondintcol = 3 where firsttextcol = 'firsttextcol';
I guess is has to do something with that? would it look like this?
UPDATE [MySheetName] SET secondintcol = ColumNumber where firsttextcol = 'RowName';
-
Monday, August 20, 2012 8:16 PM
The table name ends with $. See How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET
If you want to use the objects documented in VBA reference such as sheet or cells, you can find plenty of example codeat http://social.msdn.microsoft.com/Forums/en/exceldev/threads
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- Marked As Answer by Chester HongMicrosoft Contingent Staff, Moderator Tuesday, September 04, 2012 9:19 AM


