I'm trying to insert values into an Access database with C#, but only some of the data is being inserted. All of the data types in the database and the C# code are correct.
This code below throws an error:
Syntax ERROR in INSERT INTO statement
Code:
string insertStatement =
"INSERT INTO Car (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location, Image, Manufacturer, Description) " +
"VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location, @Image, @Manufacturer, @Description)";
OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
OleDbCommand command = new OleDbCommand(insertStatement, connection);
command.Parameters.AddWithValue("@Category", car.category.ToString());
command.Parameters.AddWithValue("@Model", car.model);
command.Parameters.AddWithValue("@CarYear", car.year);
command.Parameters.AddWithValue("@Mileage", car.mileage);
command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
command.Parameters.AddWithValue("@Availability", car.availability);
command.Parameters.AddWithValue("@Location", car.location);
command.Parameters.AddWithValue("@Image", car.image);
command.Parameters.AddWithValue("@Manufacturer", car.manufacturer);
command.Parameters.AddWithValue("@Description", car.description);
connection.Open();
command.ExecuteNonQuery();
However, if I get rid of the Image, Manufacturer, and Description then it executes with no errors. Here is the code that does not return errors and inserts only some of the data:
string insertStatement =
"INSERT INTO CAR (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location)" +
" VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location)";
OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
OleDbCommand command = new OleDbCommand(insertStatement, connection);
command.Parameters.AddWithValue("@Category", car.category.ToString());
command.Parameters.AddWithValue("@Model", car.model);
command.Parameters.AddWithValue("@Category", car.category.ToString());
command.Parameters.AddWithValue("@Model", car.model);
command.Parameters.AddWithValue("@CarYear", car.year);
command.Parameters.AddWithValue("@Mileage", car.mileage);
command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
command.Parameters.AddWithValue("@Availability", car.availability);
command.Parameters.AddWithValue("@Location", car.location);
connection.Open();
command.ExecuteNonQuery();
Here is what the data looks like (excuse the multiple entries of the same data)
Thank you for your time!