简体   繁体   中英

C# - “Syntax error in update statement” when trying to update the access database using datagridview (also using OleDbCommandBuilder)

I am displaying a data in datagridview and I am trying to update the database from datagridview. So I am using OleDbCommandBuilder to generate the update command. I get "Syntax error in update statement" when clicking on update button.

Here is my code:

 private void listBox9_SelectedValueChanged(object sender, EventArgs e)
 {
        AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
        connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\\Trip Sheet Management System\\WABCO.mdb");
        sql = "SELECT ID,[TRIP COST] FROM TMSDETAILS";
        dataAdapter = new OleDbDataAdapter(sql, connection);
        dataTable = new DataTable();
        bindingSource = new BindingSource();
        connection.Open();
        dataAdapter.Fill(dataTable);
        bindingSource.DataSource = dataTable;
        dataGridView1.DataSource = bindingSource;
        connection.Close();
  }


 private void button8_Click(object sender, EventArgs e)
    {

        commandBuilder = new OleDbCommandBuilder(dataAdapter);

        try
        {
            dataAdapter.Update(dataTable);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

When I click on update button, I get error in MessageBox saying "Syntax error in update statement"

Field 1: ID - Autonumber, primary key Field 2: TRIP COST - Number

Guys I found the answer by myself.

The error occurred because the field name contains a space ie TRIP COST.

just add the two line next to command builder statement.

commandBuilder.QuotePrefix = "[";

commandBuilder.QuoteSuffix = "]";

Here is the edited code.

 private void listBox9_SelectedValueChanged(object sender, EventArgs e)
{
    AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
    connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\\Trip Sheet Management System\\WABCO.mdb");
    sql = "SELECT ID,[TRIP COST] FROM TMSDETAILS";
    dataAdapter = new OleDbDataAdapter(sql, connection);
    dataTable = new DataTable();
    bindingSource = new BindingSource();
    connection.Open();
    dataAdapter.Fill(dataTable);
    bindingSource.DataSource = dataTable;
    dataGridView1.DataSource = bindingSource;
    connection.Close();
}


private void button8_Click(object sender, EventArgs e)
{

    commandBuilder = new OleDbCommandBuilder(dataAdapter);
    commandBuilder.QuotePrefix = "["; 
    commandBuilder.QuoteSuffix = "]";

    try
    {
        dataAdapter.Update(dataTable);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Hope it helps others. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM