Delete row from database

Discussion in 'C#' started by kermit2012, Mar 4, 2012.

  1. kermit2012

    kermit2012 New Member

    Joined:
    Jan 29, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I am try to delete a selected row from a datagridview and an MS Access Database, I can get it to delete from the datagridview but not the database, does anybody know why? here is my current code:

    Code:
    {
    int cnt = dataGridView1.SelectedRows.Count;
    for (int i = 0; i < cnt; i++)
    {
    if (this.dataGridView1.SelectedRows.Count > 0 &&
    this.dataGridView1.SelectedRows[0].Index !=
    this.dataGridView1.Rows.Count - 1)
    {
    this.dataGridView1.Rows.RemoveAt(
    this.dataGridView1.SelectedRows[0].Index);
    string queryDeleteString = "";
    OleDbCommand sqlDelete = new OleDbCommand();
    queryDeleteString = ("DELETE FROM AddAccount WHERE AccoutnID = " + i + "");
    sqlDelete.CommandText = queryDeleteString;
    sqlDelete.Connection = database;
    sqlDelete.ExecuteNonQuery();
    database.Close();
    dataGridView1.Refresh();
    }
    }
    }
    
    Thanks :)
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How do you know you didn't delete it from the database?

    Is AutoCommit on or off?

    Is the column in the DB really called "AccoutnID"? as opposed to AccountID (transposing N and T)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice