简体   繁体   中英

Unhandled exception occurs when Clicking Save Button

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?

"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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM