Mike B
Mike B
Explore posts from servers
CC#
Created by Mike B on 11/10/2024 in #help
Insert into Access Database
public Car(int vehicleID, string make, string model, int year, int mileage, decimal rentalCost, string description, string location, string availablility, string classification, string image)
{
this.vehicleID = vehicleID;
this.make = make;
this.model = model;
this.year = year;
this.mileage = mileage;
this.rentalCost = rentalCost;
this.description = description;
this.location = location;
this.availablility = availablility;
this.classification = classification;
this.image = image;
}
public Car(int vehicleID, string make, string model, int year, int mileage, decimal rentalCost, string description, string location, string availablility, string classification, string image)
{
this.vehicleID = vehicleID;
this.make = make;
this.model = model;
this.year = year;
this.mileage = mileage;
this.rentalCost = rentalCost;
this.description = description;
this.location = location;
this.availablility = availablility;
this.classification = classification;
this.image = image;
}
this is my car class
22 replies
CC#
Created by Mike B on 11/10/2024 in #help
Insert into Access Database
this is my table
22 replies
CC#
Created by Mike B on 11/10/2024 in #help
Insert into Access Database
No description
22 replies
CC#
Created by Mike B on 11/10/2024 in #help
Insert into Access Database
updated and I still recieved the same error
22 replies
CC#
Created by Mike B on 11/10/2024 in #help
Insert into Access Database
string query = "INSERT INTO Cars (VIN, Make, Model, CarYear, Mileage, RentalCost, Description, Location, Availability, Class, Image) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";


OleDbCommand cmd = new OleDbCommand(query, connection);
cmd.Parameters.AddWithValue("?", car.VehicleID);
cmd.Parameters.AddWithValue("?", car.Make);
cmd.Parameters.AddWithValue("?", car.Model);
cmd.Parameters.AddWithValue("?", car.Year);
cmd.Parameters.AddWithValue("?", car.Mileage);
cmd.Parameters.AddWithValue("?", car.RentalCost);
cmd.Parameters.AddWithValue("?", car.Description);
cmd.Parameters.AddWithValue("?", car.Location);
cmd.Parameters.AddWithValue("?", car.Availability);
cmd.Parameters.AddWithValue("?", car.Classification);
cmd.Parameters.AddWithValue("?", car.Image);
OleDbCommand command = new OleDbCommand(query, connection);

command.ExecuteNonQuery();
MessageBox.Show("Car successfully added!");

string query = "INSERT INTO Cars (VIN, Make, Model, CarYear, Mileage, RentalCost, Description, Location, Availability, Class, Image) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";


OleDbCommand cmd = new OleDbCommand(query, connection);
cmd.Parameters.AddWithValue("?", car.VehicleID);
cmd.Parameters.AddWithValue("?", car.Make);
cmd.Parameters.AddWithValue("?", car.Model);
cmd.Parameters.AddWithValue("?", car.Year);
cmd.Parameters.AddWithValue("?", car.Mileage);
cmd.Parameters.AddWithValue("?", car.RentalCost);
cmd.Parameters.AddWithValue("?", car.Description);
cmd.Parameters.AddWithValue("?", car.Location);
cmd.Parameters.AddWithValue("?", car.Availability);
cmd.Parameters.AddWithValue("?", car.Classification);
cmd.Parameters.AddWithValue("?", car.Image);
OleDbCommand command = new OleDbCommand(query, connection);

command.ExecuteNonQuery();
MessageBox.Show("Car successfully added!");

22 replies
CC#
Created by Mike B on 11/10/2024 in #help
Insert into Access Database
One sec let me update the code, I tried it and still have gotten issues
22 replies
CC#
Created by Mike B on 11/10/2024 in #help
Insert into Access Database
this is the info i got from my form and the generated inserts statement 1234, 'Porsche', '911', 2024, 23, 89.99, 'Fast', 'Lot B', 'Available', 'Sport', 'porsche.png' INSERT into Cars(VIN, Make, Model, CarYear, Mileage, RentalCost, Description, Location, Availability, Class, Image) VALUES (1234, 'Porsche', '911', 2024, 23, 89.99, 'Fast', 'Lot B', 'Available', 'Sport', 'porsche.png') Exception thrown: 'System.Data.OleDb.OleDbException' in System.Data.dll
22 replies
CC#
Created by Mike B on 11/10/2024 in #help
Insert into Access Database
internal void AddCar(Car car)
{
OleDbConnection connection = new OleDbConnection(strConnection);


try
{
connection.Open();
Console.WriteLine(car.ToString());

// Use a parameterized query to prevent SQL injection and handle special characters.
string query = "INSERT into Cars(VIN, Make, Model, CarYear, Mileage, RentalCost, Description, Location, Availability, Class, Image) " +
$"VALUES ({car.VehicleID}, '{car.Make}', '{car.Model}', {car.Year}, {car.Mileage}, {car.RentalCost}, '{car.Description}', '{car.Location}', '{car.Availability}', '{car.Classification}', '{car.Image}')";
Console.WriteLine(query);

OleDbCommand command = new OleDbCommand(query, connection);

command.ExecuteNonQuery();

}
catch (Exception ex)
{
MessageBox.Show("Addcar exception: "+ ex.Message);
}
finally
{

Console.WriteLine("connection closing");
connection.Close();
}
}
internal void AddCar(Car car)
{
OleDbConnection connection = new OleDbConnection(strConnection);


try
{
connection.Open();
Console.WriteLine(car.ToString());

// Use a parameterized query to prevent SQL injection and handle special characters.
string query = "INSERT into Cars(VIN, Make, Model, CarYear, Mileage, RentalCost, Description, Location, Availability, Class, Image) " +
$"VALUES ({car.VehicleID}, '{car.Make}', '{car.Model}', {car.Year}, {car.Mileage}, {car.RentalCost}, '{car.Description}', '{car.Location}', '{car.Availability}', '{car.Classification}', '{car.Image}')";
Console.WriteLine(query);

OleDbCommand command = new OleDbCommand(query, connection);

command.ExecuteNonQuery();

}
catch (Exception ex)
{
MessageBox.Show("Addcar exception: "+ ex.Message);
}
finally
{

Console.WriteLine("connection closing");
connection.Close();
}
}
22 replies
CC#
Created by Mike B on 11/10/2024 in #help
Insert into Access Database
how do i format the code
22 replies