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

jarlh
  • 42,561
  • 8
  • 45
  • 63
user5467760
  • 69
  • 1
  • 7
  • Forgot a `;` at the end of your `insert` statement? –  Jan 26 '16 at 12:20
  • @Kilanny That's not _have to_ as far as I know. – Soner Gönül Jan 26 '16 at 12:21
  • 1
    It could be due to incorrect parameter syntax. Check this answer http://stackoverflow.com/a/5894003/4795214 –  Jan 26 '16 at 12:24
  • I know it may be a nonsense but...did you try to remove the "[" and "]" in your table name? – Pikoh Jan 26 '16 at 12:28
  • Also use [`using` statement](https://msdn.microsoft.com/en-us/library/yh598w02.aspx) to dispose your connection and command automatically instead of calling `Close` or `Dispose` methods manually. – Soner Gönül Jan 26 '16 at 12:41

2 Answers2

2

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

sr28
  • 4,728
  • 5
  • 36
  • 67
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.

M. Schena
  • 2,039
  • 1
  • 21
  • 29