I would like to double check that a table does not contain a particular record before inserting. Data is collected from textboxes. The table Vehicule in which I need to insert to has a primary key: Vehicule_ID which is an autonumber, this doesn’t help in terms of checking or avoiding duplicate records though...
So to check that the record exists, I need in fact to check that txtMake.Text, txtModel.Text, txtColor.Text, txtbuiltYear.Text, etc.. is the same as an existing record in the table.
Using the following pseudo code might get me there: IF EXISTS (SELECT * FROM VEHICULE WHERE Make = ....and Model = ... and Color = ... etc...)
SELECT COUNT (*) FROM VEHICULE WHERE Make = ... and Model = ... etc..
But I am struggling with the correct syntax to do this in C Sharp and using MS Access 2007.
I want to check in a Try ...Catch ...Finally block like below:
private void btnInsert_Click(object sender, EventArgs e)
string InsertString = @"INSERT INTO Vehicule (Make, Model, Color, Built_year, Tank_capacity, Wheels, Seats)
VALUES (" + "'" + txtMake.Text + "','" + txtModel.Text+ "','" + txtColor.Text+ "','" + txtYear.Text+ "'," + txtTank.Text+ "," + txtWheels.Text+ "," + txtSeats.Text + " )";
OleDbCommand commandAdd = new OleDbCommand(InsertString, con);
//check if record exists
//record already exists, abort operation
//insert record into database table
txtMake.Text = "";
txtModel.Text = "";
txtColor.Text = "";
txtYear.Text = "";
txtTank.Text = "";
txtWheels.Text = "";
txtSeats.Text = "";
Could you please help me out with the right syntax to achieve this checking? Thanks.