this is my first time here so bear with me if I miss rules on posting or whatever. I'm looking for the proper way to delete a row from a database using C#. I already wrote code to delete it from the datagridview but what would I add on to completley remove it from the database?
Here is the code so far:
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
if (!row.IsNewRow)
{
dataGridView1.Rows.Remove(row);
}
MessageBox.Show("Selected rows Deleted");
}
This is what I tried out at first, thinking it would via a search:
OpenConnection();
string productType = txtDeleteProduct.Text;
MainForm frm = new MainForm();
mySqlDataAdapter = new MySqlDataAdapter("DELETE * from products WHERE ProductType= '@productType';", connection);
DataSet DS = new DataSet();
mySqlDataAdapter.Fill(DS);
dataGridView1.DataSource = DS.Tables[0];
CloseConnection();
DataGridView.Rows.Removejust remove the item from dataset, not in DB. You need to include SQL command to delete selected row(s) and useExecuteNonQuerymethod to run it.