Please help me solve these errors that I usually receive. I've created an application that reads, updates, and inserts a record into an Access database. But every time I press the Update button to update a record, I usually receive this error message "Syntax error in UPDATE statement".
On the other hand, when I also intend to 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."
I also found that when I update or insert only one field of a database, the given code works. But when I update multiple fields that's the time I experience such errors.
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!");