469,636 Members | 1,714 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

My "Delete command" deletes more than I want

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

Dec 2 '05 #1
4 1371
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
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"papaja" wrote:
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

Dec 2 '05 #2
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(primaryK ey)
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?

Dec 2 '05 #3
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
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"papaja" wrote:
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(primaryK ey)
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?

Dec 2 '05 #4
> 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.
Dec 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by solosnake | last post: by
32 posts views Thread by Christopher Benson-Manica | last post: by
6 posts views Thread by R.Z. | last post: by
1 post views Thread by Michael Ramey | last post: by
30 posts views Thread by Medvedev | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.