0

I get an error when trying to insert a record into the database.

Here's the error:

enter image description here

The Code:

connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "INSERT INTO [Booking] (TableID, BookingName, BookingNumber, BookingDate, PartySize) VALUES (@TableID, @BookingName, @BookingPhoneNumber, @BookingDate, @PartySize)";
            command.Parameters.AddRange(new OleDbParameter[] {
                    new OleDbParameter("@TableID", textBoxResTableID.Text),
                    new OleDbParameter("@BookingName", textBoxResName.Text),
                    new OleDbParameter("@BookingPhoneNumber", textBoxResNum.Text),
                    new OleDbParameter("@BookingDate", DateTime.Today),
                    new OleDbParameter("@PartySize", textBoxResPartySize.Text)
                });

            command.ExecuteNonQuery();
            connection.Close();

The DB:

enter image description here

5
  • Forgot a ; at the end of your insert statement? Commented Jan 26, 2016 at 12:20
  • @Kilanny That's not have to as far as I know. Commented Jan 26, 2016 at 12:21
  • 1
    It could be due to incorrect parameter syntax. Check this answer stackoverflow.com/a/5894003/4795214 Commented Jan 26, 2016 at 12:24
  • I know it may be a nonsense but...did you try to remove the "[" and "]" in your table name? Commented Jan 26, 2016 at 12:28
  • Also use using statement to dispose your connection and command automatically instead of calling Close or Dispose methods manually. Commented Jan 26, 2016 at 12:41

2 Answers 2

2

Looks like TABLEID may be a reserved word according to this. To get round it enclose it in [].

Sign up to request clarification or add additional context in comments.

4 Comments

Wow. I didn't expect this as a reserved keyword. Nice find. +1
@SonerGönül - it was a surprise to me as well. I almost didn't look as I thought it would be a dead end.
This solved it! I cannot believe it, was racking my brain! massive thank you!
@user5467760 - no problem. Glad I could help.
0

I had same problem and solved it adding parameters with following syntax:

 command.Parameters.Add("@UserName", OleDbType.BSTR).Value = username;

EDIT: pasted wrong command first.

2 Comments

But why do you think this can solve OP's problem? What is the problem anyway?
I have no clue why. But with his method i had the same error. Not shure if it's the solution, but worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.