Reputation: 9268
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:
Upvotes: 0
Views: 2758
Reputation: 5806
--- 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(?,?)";
Upvotes: 1
Reputation: 1221
You need the values part of the insert into statement.
insert into fuel_limit (fuel_limit_code, fuel_limit_description) values (?fuel_limit_code, ?fuel_limit_description)
Also, it looks like your you need to use the description parameter when setting the value in the line right above the ExecuteNonQuery statment.
Upvotes: 4