Search code examples
c#databasevisual-studio-2010ms-access-2007oledb

OleDbException was unhandled: Syntax Error (missing operator) In Query Expression


I am new in creating an application using Visual Stuido 2010 C# and Microsft Access 2007. I am planning to create an application where the user can add data to the database(MS-Access). But I got an error stating that "Syntax Error (missing operator) In Query Expression". I really can't find what's the problem with my code.

This is my code in adding data to the database:

private void buttonSaveFuelLimit_Click(object sender, EventArgs e)
    {
        string MyConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\KKKKK\Documents\Visual Studio 2010\Projects\Trial\Trial\gxi.accdb";
        OleDbConnection connection = new OleDbConnection(MyConString);
        OleDbCommand command = connection.CreateCommand();
        command.Connection = connection;
        using (OleDbConnection conn = new OleDbConnection(MyConString))
        {
            connection.Open();
            using (OleDbCommand com = connection.CreateCommand())
            {
                command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description) values(?fuel_limit_code, ?fuel_limit_description)";
                command.Parameters.Add(new OleDbParameter("?fuel_limit_code", OleDbType.VarChar));
                command.Parameters.Add(new OleDbParameter("?fuel_limit_description", OleDbType.VarChar));
                command.Parameters["?fuel_limit_code"].Value = textBoxFuelLimitCode.Text;
                command.Parameters["?fuel_limit_description"].Value = textBoxFuelLimitDesc.Text;
                command.ExecuteNonQuery();
                MessageBox.Show("Data Saved");
            }
        }
    }

This is the screen shot of the error message: enter image description here


Solution

  • --- Edited answer --- following will work

    
    command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description) values(?, ?)";
    command.Parameters.AddWithValue(new OleDbParameter("@fuel_limit_code",textBoxFuelLimitCode.Text));
    command.Parameters.AddWithValue(new OleDbParameter("@fuel_limit_desc", textBoxFuelLimitDesc.Text));
    command.ExecuteNonQuery();
    
    
    

    --- old answer ---

    here is the line that needs correction

    
    command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description)";
    //should ne
    command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description) values(?,?)";