My "Delete command" deletes more than I want

P

papaja

Hi,
I posted this on asp.net forums already, but nobody answered it. Here
is direct link: http://forums.asp.net/1124640/ShowPost.aspx.

Here is the question:

I'm using this code in delete command of datagrid:
****************
Dataset1.DSataTable1.Rows(e.Item.ItemIndex).Delete()
adp.Update(DataSet1)
DataGrid1.DataBind()
****************

And it works, but whenever I refresh page another row is deleted. How
to stop this from happening?

Thanks
 
G

Guest

Instead of using the ItemIndex to identify the row to be deleted, create a
primary key field on your table, use a DataKeyField to search for a row to be
deleted (i.e. delete a row based on its primary key rather than its
position), e.g.

Dim primaryKey As String= DataGrid1.DataKeys[e.Item.ItemIndex]
'Notice you cannot use the Find method on the rows collection unless you have
'a primary key set on your DataTable, e.g,
'Dim keys(0) As DataColumn
'keys(0) = dt.Columns(0) 'select the first column as the primary key
'Dataset1.DSataTable1.PrimaryKey = keys

Dim foundRow As DataRow= Dataset1.DSataTable1.Rows.Find(primaryKey)
If Not foundRow Is Nothing Then 'if the row is not deleted yet
foundRow.Delete()
adp.Update(DataSet1)
DataGrid1.DataBind()
Else 'the row was deleted already
Response.Write("Cannot delete the row because it was deleted
already")
End If
 
P

papaja

Here is entire subroutine as it is in my page.

It does not delete rows, and sometimes it reports the following error:
Concurrency violation: the DeleteCommand affected 0 records.

*******************************
Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.DeleteCommand

Dim primaryKey As String = DataGrid1.DataKeys(e.Item.ItemIndex)
'Notice you cannot use the Find method on the rows collection
unless you have
'a primary key set on your DataTable, e.g,
Dim keys(0) As DataColumn
keys(0) = DsLITAdminNovosti.tblLITNovosti.Columns(0) 'select
the first column as the primary key
DsLITAdminNovosti.tblLITNovosti.PrimaryKey = keys

Dim foundRow As DataRow =
DsLITAdminNovosti.tblLITNovosti.Rows.Find(primaryKey)
If Not foundRow Is Nothing Then 'if the row is not deleted
yet
foundRow.Delete()
adp.Update(DsLITAdminNovosti)
DataGrid1.DataBind()
End If
End Sub
*******************************
Did I misunderstood something?
 
G

Guest

Yes, I see what happend. My mistake. My comment about the primary key was
mainly intended as a reminder that you cannot search the table using the
rows.find method unless you have a primary key. You got a concurrency
exception because you changed the table definition (by adding a primary key)
then attempted to update the table on the database (which the DataAdapter
would not do).

Since your table defintion does not have a primary key on it, you can select
the row using a filter on the dataview like this:

Dim MyDataKey As String = DataGrid1.DataKeys(e.Item.ItemIndex)
Dim tbl As DataTable
'this will filter the datatable based on the column designated as chosen
datakey
Dim dvFound As New DataView(tbl, "FieldNameOfPrimaryKey='" & MyDataKey &
"'", Nothing, DataViewRowState.CurrentRows)
If dvFound.Count = 1 Then 'if there is a record that match this Datakey
dvFound(0).Delete()
adp.Update(DsLITAdminNovosti)
DataGrid1.DataBind()
End If
 
H

Hans Kesting

Hi,
I posted this on asp.net forums already, but nobody answered it. Here
is direct link: http://forums.asp.net/1124640/ShowPost.aspx.

Here is the question:

I'm using this code in delete command of datagrid:
****************
Dataset1.DSataTable1.Rows(e.Item.ItemIndex).Delete()
adp.Update(DataSet1)
DataGrid1.DataBind()
****************

And it works, but whenever I refresh page another row is deleted. How
to stop this from happening?

Thanks

The page you get after you deleted the first row is the result of a
command "delete the fifth row from the table". When you refresh the
page, you effectively send that command again, so an extra row is
deleted. You will also get a waring about resubmitting the page.

What I do often in cases like this: after the delete-action, redirect
to the listview. That way it's a plain listview, without hidden
"delete" commands. When you refresh that page, nothing special happens
(and the warning is gone).

Hans Kesting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top