Hello out there! I've tried lots of editing in this code yet I still receive the following errors.

1. Every time I press the Update button to update a record, I usually receive this error message “Syntax error in UPDATE statement”.
2. When I press the Save New button to insert a new record in the database, I usually receive this error message “Syntax error in INSERT INTO statement.”

My Access database compose of the following field names: IDNo, Course, Age, Year (all declared as TEXT). The following are the selected codes of my application that experiences the error:

private void btnUpdate_Click(object sender, EventArgs e){
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\EBabes\DataTesting.mdb";
OleDbConnection conn = new OleDbConnection(ConnectionString);
OleDbCommand cmd = new OleDbCommand("UPDATE StudTable SET Course=?, Year=?, Age=? WHERE IDNo = ?", conn);
cmd.Parameters.Add("@Course", OleDbType.VarChar).Value = txtCourse.Text;
cmd.Parameters.Add("@Year", OleDbType.VarChar).Value = txtYear.Text;
cmd.Parameters.Add("@Age", OleDbType.VarChar).Value = txtAge.Text;
cmd.Parameters.Add("@IDNo", OleDbType.VarChar).Value = txtID.Text;

conn.Open();
cmd.ExecuteNonQuery(); 
conn.Close();

clearForm();
MessageBox.Show("Record updated!");
}
private void btnSaveNew_Click(object sender, EventArgs e){
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\EBabes\DataTesting.mdb";
OleDbConnection conn = new OleDbConnection(ConnectionString);
OleDbCommand cmd = new OleDbCommand("INSERT INTO StudTable (IDNo, Course, Year, Age) VALUES (?, ?, ?, ?),conn");
cmd.Parameters.Add("@IDNo", OleDbType.VarChar).Value = txtID.Text;
cmd.Parameters.Add("@Course", OleDbType.VarChar).Value = txtCourse.Text;
cmd.Parameters.Add("@Year", OleDbType.VarChar).Value = txtYear.Text;
cmd.Parameters.Add("@Age", OleDbType.VarChar).Value = txtAge.Text;

conn.Open();
cmd.ExecuteNonQuery(); 
conn.Close();

clearForm();
MessageBox.Show("Record updated!"); 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.