0

I am trying to update an MS-Excel file using OledbDataAdapter and I am getting "syntax error in Insert Into statement"

This is my code:

  private void btnLoadPremiumDetail_Click(object sender, RoutedEventArgs e) {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            string fileName = "";
            if (openFileDialog.ShowDialog() == true)
                fileName = openFileDialog.FileName;
            else
                return;
            string sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=No;IMEX=1'";
            this.connection = new OleDbConnection(sConnection);
            this.connection.Open();
            var dtTablesList = connection.GetSchema("Tables");
            var sSheetName = "";
            if (dtTablesList.Rows.Count > 0)
                sSheetName = dtTablesList.Rows[0]["TABLE_NAME"].ToString();
            dtTablesList.Clear();
            dtTablesList.Dispose();
            if (!string.IsNullOrEmpty(sSheetName)) {
                var command = new OleDbCommand("Select * From [" + sSheetName + "]", connection);
                this.adapter = new OleDbDataAdapter(command);
                new OleDbCommandBuilder(this.adapter);
                this.table = new DataTable();
                this.adapter.Fill(table);
                this.dgExcelData.ItemsSource = table.DefaultView;
            }

        }

 private void btnSave_Click(object sender, RoutedEventArgs e) {
            this.adapter.Update(this.table);
            this.lblStatus.Content = "Data saved";
        }
3
  • What is the full error? Commented Dec 15, 2014 at 11:26
  • An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: Syntax error in INSERT INTO statement. Commented Dec 15, 2014 at 11:28
  • I'm on the phone so I might be missing on something but the code you submitted only has a select statement. Donno how you get an error related to Insert Into! Commented Dec 15, 2014 at 12:01

1 Answer 1

0

I have decided to give ClosedXML a try: http://closedxml.codeplex.com/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.