Delete not saved in database!!!

maia_18

New member
Joined
Jul 22, 2011
Messages
4
Programming Experience
Beginner
Hello everyone!!!

i just stumped upon a stupid problem...

i have an application that uses a datagridview to display contents from database...
but i just can't apply the delete function to the database...
when i click my delete menustrip item, the row i've selected gets deleted... but returns after i've pressed another button on my form!!!
please help...
Thanks in advance...

codecodecode:

VB.NET:
Private Sub DeleteItemToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteItemToolStripMenuItem.Click        dgvLadies.DataSource = vbNull
        dsLadies = New DataSet()


        daProducts.Fill(dsLadies, "products")
        dgvLadies.DataSource = dsLadies.Tables(0)
        dgvLadies.Visible = True


        If MsgBox("Do you want to delete this row ?", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            dsLadies.Tables("products").Rows.RemoveAt(dgvLadies.CurrentRow.Index)
            dgvLadies.Rows.RemoveAt(dgvLadies.CurrentRow.Index)
        End If
        daProducts.Update(dsLadies, "products")
    End Sub
 
The problem is that you aren't deleting anything. Deleting and removing are very different in this case. Removing a row from a DataTable means taking it out as though it was never added in the first place. Deleting it means leaving it in but setting its RowState to Deleted. When you call Update on your data adapter, if the row isn't in the DataTable then how can it be deleted from the database? The row needs to be in the DataTable so the adapter knows what row to delete. So, you need to get the desired DataRow and call its Delete method.

Apart from that, why are you retrieving data from the database when the user wants to delete something? Surely you already have the data at that point, sop why the extra query? I would think that what you should be doing is getting the data up front, bind it to a BindingSource and binding that to the grid. When the user selects a row in the grid, you can delete it by simply calling RemoveCurrent on the BindingSource. That will call Delete on the DataRow internally.
 
That was a stupid problem after all..
Thanks jmcilhinney for the reply!!!!
i was cleared of the difference between removing and deleting...

oh, and i also deleted the extra code...

Thanks very much!!!!
 
Back
Top