0

I have a show delete button of each row for delete from database permanently, how can delete data from DB ?

The following code shows delete button of each row. I have user Oracle Database Where I have write query for delete ?

DataGridViewLinkColumn Deletelink = new DataGridViewLinkColumn();
Deletelink.UseColumnTextForLinkValue = true;
Deletelink.HeaderText = "delete";
Deletelink.DataPropertyName = "lnkColumn";
Deletelink.LinkBehavior = LinkBehavior.SystemDefault;
Deletelink.Text = "Delete";
dataGridView1.Columns.Add(Deletelink);

I have write the code for delete but I have fail

private void dataGridView1_CellContentClick(object sender DataGridViewCellEventArgs e)
{
  if (e.ColumnIndex == 5)
  {
    empid = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["empid"].Value);
    OleDbDataAdapter adp = new OleDbDataAdapter("delete  from apps where appsid= '"+empid+"'", con);
    DataTable dt = new DataTable();
    adp.Fill(dt);
    dataGridView1.DataSource = dt;
    dataGridView1.Refresh();
LarsTech
  • 80,625
  • 14
  • 153
  • 225
khan
  • 79
  • 1
  • 9

1 Answers1

0

A delete command issued as constructor for a OleDbDataAdapter is not correct. However the adapter will execute the command nevertheless. But surely you cannot expect a delete command to be used to fill a datatable.
You still need a SELECT.

Usually this is done removing the row from the grid (this marks the underlying row as deleted then, when you want to save the changes to the database, you should use the orginal DataAdapter and the original DataTable used to initially fill the grid to issue the Update method

This is well explained here Using OleDbDataAdapter to Update a DataTable

Or you could do the update yourself using a separate command and reloading the datatable

// Delete the record on the db
empid = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["empid"].Value);
OleDbCommand cmd = new OleDbCommand("delete from apps where appsid= @id", con);
cmd.Parameters.Add("@id", OleDbType.VarWChar).Value = empid;
cmd.ExecuteNonQuery();

// reload the grid with the updated info from the db
OleDbDataAdapter da = new OleDbDataAdapter("Select * from apps", con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();

This of course has the drawback to hit two times the database and reloading everything each time you delete a row.

Steve
  • 213,761
  • 22
  • 232
  • 286