Reputation: 141
private void btnSave_Click(object sender, EventArgs e)
{
if (!ValidInput())
return;
using (var conn = new OleDbConnection(DatabaseObjects.ConnectionString))
{
conn.Open();
command = new OleDbCommand("insert into Students (RollNo, SName, FName, DOB, Class, Section) values (@RollNo, @SName, @FName, @DOB, @Class, @Section)", conn);
command.Parameters.Add(new OleDbParameter("RollNo", txtRollNo.Text));
command.Parameters.Add(new OleDbParameter("SName", txtSName.Text));
command.Parameters.Add(new OleDbParameter("FName", txtFName.Text));
command.Parameters.Add(new OleDbParameter("DOB", dtpDOB.Text));
command.Parameters.Add(new OleDbParameter("Class", cmbClass.Text));
command.Parameters.Add(new OleDbParameter("Section", cmbSection.Text));
command.ExecuteNonQuery();
MessageBox.Show("Saved");
}
}
When I Click Save Button this error is rising (Insert Into Syntax Error).
I have troubleshooted the code, I Find that if Field Section and Parameter @Section is removed from insert statement. Then Code works fine. But I want to Get ComboBox(CmbSection) Value of it and also to store on Database. How Can I?
Upvotes: 0
Views: 103
Reputation: 218960
"Section" is a reserved word in MS Access, so its use here is confusing the query parser.
You can explicitly tell the query what identifiers are referring to database objects (columns, tables, etc.) by wrapping them in square brackets:
INSERT INTO [Students]
([RollNo], [SName], [FName], [DOB], [Class], [Section])
VALUES (@RollNo, @SName, @FName, @DOB, @Class, @Section)
Upvotes: 3