473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1519
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
6
by: R.Z. | last post by:
i'm using a class from some api that is said to automatically call its destructor when its out of scope and deallocate memory. i create instances of this class using "new" operator. do i have to...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
8
by: John Baker | last post by:
Hi: Access 2000 W98! I have a table with numerous records in it, and am attempting to delete certain records that have been selected from it. These are selected based on the ID number in a...
1
by: Michael Ramey | last post by:
Hi, I'm dynamically creating a table of "delete" imagebuttons, that correspond to files on the webserver. I want to respond to clicks of these buttons, so I know to know what file to delete. ...
7
by: Alex Maghen | last post by:
I have a DataGrid control with a LinkButton command column that deletes the row. What I want to do is set it up so that there's a client-side Confirm alert BEFORE the actual Delete command gets...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
1
by: psyvanz | last post by:
any code that can delete all records in just one click if you have a dataenvironment code it more good.. cause im working in this kind of code. like this: single delete code (not all in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.