Search code examples
c#ms-accessoledb

Access table update fail


I use the following code to fill a DataGridView from an Access Database. After a Save button is clicked if the Data Grid is updated, the Database saves the data.

The strange thing is that this works for 2 out of 3 Data Tables. For the last one it throws an exception:

Syntax error in INSERT INTO statement

public Tables(string tabName)
{
    InitializeComponent();
    this.Text = tabName;
    this.query = string.Format("SELECT *" + " FROM [{0}]", tabName);
    conn.Open();
    detailTable = new DataTable();
    string tableName = tabName;
    string query = string.Format("SELECT * FROM [{0}]", tableName);
    OleDbDataAdapter detailAdapter = new OleDbDataAdapter(query, conn);
    if (detailAdapter != null)
    {
        detailAdapter.Fill(detailTable);
    }
    DataGridView.DataSource = detailTable;
    conn.Close();
}

private void BtnSave_Click(object sender, EventArgs e)
{
    OleDbCommand comm = new OleDbCommand(query, conn);
    OleDbDataAdapter adapter = new OleDbDataAdapter(comm);
    OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
    adapter.Update(detailTable);
}

Even if the words are reserved keywords (although I searched them) there is no way of that to be the issue. More information the tabName can be "Partners", "Salaries", "Descriptions" and "Accounts". It doesn't work only on "Partners".


Solution

  • Okay, turns out I was wrong and I apologize for my stubbornness. The problem was in the e-mail column and I did not see why it had a problem with the dash mark.