the code below is what i wrote in my delete button, but the problem is i dont kn
ID: 3776583 • Letter: T
Question
the code below is what i wrote in 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, it works but it would delete all rows that has the word 'New York' in it and i dont want that i just want it to delete the specific one 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:UserssereneDesktopsamb - CopysambPublic.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)
Explanation / Answer
When you do sql = "delete from Incidents where Location = 'New York'"; it will delete all fields which contains location "New York"
if you want to delete a particular field which contains "New York" do using of composite key
composite key : composite key is used to identify a field with more than one attribute
let us take you have table with 3 attributes id, name, location
talbename : incidents
id name location
1 php new york
2 java new york
if you do "delete from incidents where location='new york'", it will delete all field which contains new york
instead doing follow below query
"delete from incidents where location='new york' and id='1'" then it will delete only first field.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.