954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problems on Insert and Update

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!"); 
}
ebabes
Junior Poster in Training
73 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You