I’m having great difficulty in inserting a record to my access database. I’ve tried the query in access and it inserts fine. I’ve also tried the query in the query builder and it works too however if I run this code it claims to have inserted the record into the database, but when I check the database there is no sign of a new record.
oleDbCommandCreateAppointment.Parameters["appointmentDate"].Value = "06/04/2012";
oleDbCommandCreateAppointment.Parameters["timeSlotID"].Value ="21";
oleDbCommandCreateAppointment.Parameters["startTime"].Value = "09:00";
oleDbCommandCreateAppointment.Parameters["employeeID"].Value ="1";
oleDbCommandCreateAppointment.Parameters["clientID"].Value ="1";
oleDbCommandCreateAppointment.Parameters["assistantID"].Value ="1";
oleDbCommandCreateAppointment.Parameters["appointmentType"].Value = "Quote";
oleDbCommandCreateAppointment.Parameters["appointmentFlag"].Value = "Booked";
try
{
oleDbConnection.Open();
int rows = oleDbCommandCreateAppointment.ExecuteNonQuery();
MessageBox.Show("Rows inserted " + rows.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
oleDbConnection.Close();
}
SQL Command
INSERT INTO tblAppointments
(appointmentDate, timeSlotID, startTime, employeeID, clientID, assistantID, appointmentType, appointmentFlag)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
Many thanks
You need to associate the connection with the db command object:EDIT – This
may havehas to do with the parameter ordering, try this:NOTE – Something I learned while working on this, you can’t use named parameters with ODBC and OleDB commands, only positional parameters (see Table 6) and this link points out that the OleDbCommand object only supports positional parameters when the command type is
Text. Positional parameters are order dependent.