the code below is my delete button but the problem is i dont

the code below is my delete button, but the problem is i dont know how to make it specific for the exact row i want to delete, because when i run it works but it would delete all rows that has the \'New York\' and i dont want that i just want it to delete the specific one i want i clicked on from the database. How to modify it ?

string connetionString = null;
OleDbConnection connection;
OleDbDataAdapter oledbAdapter = new OleDbDataAdapter();
string sql = null;
connetionString = @\"Provider = Microsoft.ACE.OLEDB.12.0; Data Source=C:\\Users\\serene\\Desktop\\samb - Copy\\samb\\Public.accdb; Persist Security Info = False;\";
connection = new OleDbConnection(connetionString);
sql = \"delete from Incidents where Location = \'New York\'\";
try
{
connection.Open();
oledbAdapter.DeleteCommand = connection.CreateCommand();
oledbAdapter.DeleteCommand.CommandText = sql;
oledbAdapter.DeleteCommand.ExecuteNonQuery();
MessageBox.Show(\"Row(s) Deleted !! \");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Note: I have 4 tables in the databaseans their name is (Incident Report No,Occurrence Time,Gategory of Incident,Location)

Solution

For deleting you are using the following query:

sql = \"delete from Incidents where Location = \'New York\'\";

I guess the table mentioned in the query \"Incidents\" is the \"Incident Report No\" as given by you above.

So, what you can do is, the \"Incidents\" table will have a primary key attached to it.

So, the row which you want to delete, i.e. the one which you have clicked in the database, pick the primary key value from that row and then modify the delet query as below:

sql = \"delete from Incidents where primary_key=123 and Location = \'New York\'\";

supposing primary_key is the name of the primary key column and the value for that is \"123\" for the corresponding row which you selected.

the code below is my delete button, but the problem is i dont know how to make it specific for the exact row i want to delete, because when i run it works but i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site