the code below is my delete button, but the problem is i dont know how to make i
ID: 3776561 • Letter: T
Question
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: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
Solution:
In this code assign the textbox field in variable.
Modified Code:
string loc=”New York”;
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 =’”+loc+”’";
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());
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.