Dato Pirtskhalava

Dato Pirtskhalava

  • 1.7k
  • 76
  • 1.2k

Delete from database

Oct 5 2014 12:50 PM
hello I want to ask how to  delete selected row  from the database
this is the code
 
 //this is the rowheader mouse click function 
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (dataGridView1.SelectedCells.Count > 0)
{
frmquestion que = new frmquestion();
que.Show();
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
//convert to stringh
string lb1 = Convert.ToString(selectedRow.Cells[0].Value);
string lb2 = Convert.ToString(selectedRow.Cells[1].Value);
string lb3 = Convert.ToString(selectedRow.Cells[2].Value);
string lb4 = Convert.ToString(selectedRow.Cells[3].Value);
string lb5 = Convert.ToString(selectedRow.Cells[4].Value);
string lb6 = Convert.ToString(selectedRow.Cells[5].Value);
string lb7 = Convert.ToString(selectedRow.Cells[6].Value);
string lb8 = Convert.ToString(selectedRow.Cells[7].Value);
string lb9 = Convert.ToString(selectedRow.Cells[8].Value);
string lb10 = Convert.ToString(selectedRow.Cells[9].Value);
string lb11 = Convert.ToString(selectedRow.Cells[10].Value);
string lb12 = Convert.ToString(selectedRow.Cells[11].Value);
string lb13 = Convert.ToString(selectedRow.Cells[12].Value);
string lb14 = Convert.ToString(selectedRow.Cells[13].Value);
string lb15 = Convert.ToString(selectedRow.Cells[14].Value);
string lb16 = Convert.ToString(selectedRow.Cells[15].Value);
string lb17 = Convert.ToString(selectedRow.Cells[16].Value);
string lb18 = Convert.ToString(selectedRow.Cells[17].Value);
string lb19 = Convert.ToString(selectedRow.Cells[18].Value);
string lb20 = Convert.ToString(selectedRow.Cells[19].Value);
string lb21 = Convert.ToString(selectedRow.Cells[20].Value);
string lb22 = Convert.ToString(selectedRow.Cells[21].Value);
//Coppy string in lbl
label1.Text = lb1;
label3.Text = lb2;
label4.Text = lb3;
label5.Text = lb4;
label6.Text = lb5;
label7.Text = lb6;
label8.Text = lb7;
label9.Text = lb8;
label10.Text = lb9;
label11.Text = lb10;
label12.Text = lb11;
label13.Text = lb12;
label14.Text = lb13;
label15.Text = lb14;
label16.Text = lb15;
label17.Text = lb16;
label18.Text = lb17;
label19.Text = lb18;
label20.Text = lb19;
label21.Text = lb20;
label22.Text = lb21;
label23.Text = lb22;
label23.Text = lb22;
print(); //Print function it works
if (MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
{
delete_records();   
}
this.Dispose();
}
}
 
 delete records function but doesn't work 
 
 
 
public void delete_records()
{
try
{
int RowsAffected = 0;
con = new OleDbConnection(cs);
con.Open();
string cq = "delete from Traceability where ID'" + label1.Text + "'";   //if i make ID* it delete all my db how i can make it 
cmd = new OleDbCommand(cq);
cmd.Connection = con;
RowsAffected = cmd.ExecuteNonQuery();
if (RowsAffected > 0)
{
MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
 
 
 
 

Answers (2)