Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Delete row from database (http://www.go4expert.com/forums/delete-row-database-t27950/)

kermit2012 5Mar2012 03:17

Delete row from database
 
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 :)

xpi0t0s 6Mar2012 13:28

Re: Delete row from database
 
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)


All times are GMT +5.5. The time now is 23:36.