473,399 Members | 3,919 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,399 software developers and data experts.

Prevent delete in DataGrid by throwing exception, where I can Catch it?

I read articles that suggest preventing delete by throwing Exception from
RowDeleting Event.
I not understand where I can catch this Error?
Nov 21 '05 #1
10 5534
MttC,

Therefore you should declare your datatable "withevents"

http://msdn.microsoft.com/library/de...etingtopic.asp

I hope this helps?

Cor
Nov 21 '05 #2
Hi,

Here is an example that allows you to prevent the user from
deleting a row from a datagrid.

http://www.onteorasoftware.com/downloads/delete.zip

Ken
--------------
"mttc" <s@hotmail.com> wrote in message news:41******@news.012.net.il...
I read articles that suggest preventing delete by throwing Exception from
RowDeleting Event.
I not understand where I can catch this Error?

Nov 21 '05 #3
Ken,

I did not read your sample again, however I thought to remember that it was
about a "remove" which is done by the combination of the del key.

(A remove is never updated in a database)

Just to be sure and maybe you can use that word next time, because there are
persons including me and a very active AdoNet one, who had a lot of problems
with that in the beginning.

Cor

"Ken Tucker [MVP]" <vb***@bellsouth.net>
...
Hi,

Here is an example that allows you to prevent the user from
deleting a row from a datagrid.

http://www.onteorasoftware.com/downloads/delete.zip

Ken
--------------
"mttc" <s@hotmail.com> wrote in message news:41******@news.012.net.il...
I read articles that suggest preventing delete by throwing Exception from
RowDeleting Event.
I not understand where I can catch this Error?

Nov 21 '05 #4
MttC,

Reading Kens answer, I think my direct answer on your question and not on
the subject does not fit for this.

The delete by a datagrid using the keys is a remove, which is not a delete.

(A remove removes a row completly from a datatable, a delete update the
rowstate, which will be done by the next update as well in a dataset)

Cor
Nov 21 '05 #5
Wrong. It's exactly delete logically, not remove.

"Cor Ligthert" wrote:
MttC,

Reading Kens answer, I think my direct answer on your question and not on
the subject does not fit for this.

The delete by a datagrid using the keys is a remove, which is not a delete.

(A remove removes a row completly from a datatable, a delete update the
rowstate, which will be done by the next update as well in a dataset)

Cor

Nov 21 '05 #6
Thanks all

I see that this issue is interesting all developers. By the way why the
Microsoft deals with this need directly?

Ok, let’s clear my q, I talk about Prevent delete, but that not exactly my
target.
I need to give the user tow way to delete from the grid, by Del key and by
Button.
I want to give user ability to cancel or confirm by MsgBox. And I want to
Update
DB immediately

The problam is that RowDeleting event not comes with:
ByRef Cancel parameter as Boolean
(This basic parameter missing in many event in Dot.Net like KeyPress)

we can resolve this problem in many ways:

A. Drive new class of DataGrid like this:
http://www.onteorasoftware.com/downloads/delete.zip
B. Drive new class of DataTable with new version of RowDeleteng,RowChangin
that include:
ByRef Cancel parameter as Boolean.
C. Throwing exception within the RowDeleted event (I find it in some
microsoft article)
D. Use with RejectChange. This way is problematical. You need to avoid
deadlock.
If you call Adepter.update from within the events, you should know, that
calls to
AcceptChange, that call again to RowDeleted. heare compleate code in
shortly:
===============
Write in class:
Dim ConfirmDelete As Boolean = False
Write in form Load
AddHandler Ds.Tables("xxx").RowDeleted, AddressOf OnDel
AddHandler DsTorm.Tables("xxx").RowDeleting, AddressOf OnDeleting

Sub OnDeleting(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
If MsgBox(msg,MsgBoxStyle.OKCancel) = MsgBoxResult.OK Then
ConfirmDelete = True
Else
ConfirmDelete = False
End If
End Sub

Sub OnDel (ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
If e.Action = DataRowAction.Commit Then Exit Sub
If (Not ConfirmDelete) Then
e.Row.RejectChanges()
Exit Sub
End If

ConfirmDelete = False

Try
Dim s As String = "delete from tikshoret where [id]=" & e.Row("id",
DataRowVersion.Original)
Dim c As New SqlCommand(s, Conn)
c.ExecuteNonQuery()
e.Row.AcceptChanges()
Catch ex As Exception
e.Row.RejectChanges()
End Try

================

Now my q is if I go with C solution (see above) where I can catch this
exception.
In other word I can ask:
Where I can catch Exception that arise with built in control-responds?
Nov 21 '05 #7
Just wonder about your soltion D: Where is the deadlock from? As I see, it's
just a single thread application. I will prefer rejectChanges if there is no
potential deadlock.

"mttc" wrote:
Thanks all

I see that this issue is interesting all developers. By the way why the
Microsoft deals with this need directly?

Ok, let’s clear my q, I talk about Prevent delete, but that not exactly my
target.
I need to give the user tow way to delete from the grid, by Del key and by
Button.
I want to give user ability to cancel or confirm by MsgBox. And I want to
Update
DB immediately

The problam is that RowDeleting event not comes with:
ByRef Cancel parameter as Boolean
(This basic parameter missing in many event in Dot.Net like KeyPress)

we can resolve this problem in many ways:

A. Drive new class of DataGrid like this:
http://www.onteorasoftware.com/downloads/delete.zip
B. Drive new class of DataTable with new version of RowDeleteng,RowChangin
that include:
ByRef Cancel parameter as Boolean.
C. Throwing exception within the RowDeleted event (I find it in some
microsoft article)
D. Use with RejectChange. This way is problematical. You need to avoid
deadlock.
If you call Adepter.update from within the events, you should know, that
calls to
AcceptChange, that call again to RowDeleted. heare compleate code in
shortly:
===============
Write in class:
Dim ConfirmDelete As Boolean = False
Write in form Load
AddHandler Ds.Tables("xxx").RowDeleted, AddressOf OnDel
AddHandler DsTorm.Tables("xxx").RowDeleting, AddressOf OnDeleting

Sub OnDeleting(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
If MsgBox(msg,MsgBoxStyle.OKCancel) = MsgBoxResult.OK Then
ConfirmDelete = True
Else
ConfirmDelete = False
End If
End Sub

Sub OnDel (ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
If e.Action = DataRowAction.Commit Then Exit Sub
If (Not ConfirmDelete) Then
e.Row.RejectChanges()
Exit Sub
End If

ConfirmDelete = False

Try
Dim s As String = "delete from tikshoret where [id]=" & e.Row("id",
DataRowVersion.Original)
Dim c As New SqlCommand(s, Conn)
c.ExecuteNonQuery()
e.Row.AcceptChanges()
Catch ex As Exception
e.Row.RejectChanges()
End Try

================

Now my q is if I go with C solution (see above) where I can catch this
exception.
In other word I can ask:
Where I can catch Exception that arise with built in control-responds?

Nov 21 '05 #8
Hi,

Use the dataadapters rowupdating event. My example deletes a
row from a datatable and cancels it from being sent to the database.
http://msdn.microsoft.com/library/de...atingtopic.asp

http://msdn.microsoft.com/library/de...atingtopic.asp

Dim WithEvents ds As New DataSet

Dim WithEvents da As OleDbDataAdapter

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Categories", conn)

da.Fill(ds, "Categories")

DataGrid1.DataSource = ds .Tables("Categories")

ds.Tables("Categories").Rows(0).Delete()

da.Update(ds, "Categories")

End Sub

Private Sub da_RowUpdating(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatingEventArgs) Handles da.RowUpdating

MessageBox.Show(e.StatementType.ToString)

e.Status = UpdateStatus.SkipCurrentRow

End Sub

Private Sub da_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles da.RowUpdated

MessageBox.Show("Deleted")

End Sub

Ken

-------------------------------------

"mttc" <s@hotmail.com> wrote in message news:41********@news.012.net.il...
Thanks all

I see that this issue is interesting all developers. By the way why the
Microsoft deals with this need directly?

Ok, let's clear my q, I talk about Prevent delete, but that not exactly my
target.
I need to give the user tow way to delete from the grid, by Del key and by
Button.
I want to give user ability to cancel or confirm by MsgBox. And I want to
Update
DB immediately

The problam is that RowDeleting event not comes with:
ByRef Cancel parameter as Boolean
(This basic parameter missing in many event in Dot.Net like KeyPress)

we can resolve this problem in many ways:

A. Drive new class of DataGrid like this:
http://www.onteorasoftware.com/downloads/delete.zip
B. Drive new class of DataTable with new version of RowDeleteng,RowChangin
that include:
ByRef Cancel parameter as Boolean.
C. Throwing exception within the RowDeleted event (I find it in some
microsoft article)
D. Use with RejectChange. This way is problematical. You need to avoid
deadlock.
If you call Adepter.update from within the events, you should know, that
calls to
AcceptChange, that call again to RowDeleted. heare compleate code in
shortly:
===============
Write in class:
Dim ConfirmDelete As Boolean = False
Write in form Load
AddHandler Ds.Tables("xxx").RowDeleted, AddressOf OnDel
AddHandler DsTorm.Tables("xxx").RowDeleting, AddressOf OnDeleting

Sub OnDeleting(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
If MsgBox(msg,MsgBoxStyle.OKCancel) = MsgBoxResult.OK Then
ConfirmDelete = True
Else
ConfirmDelete = False
End If
End Sub

Sub OnDel (ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
If e.Action = DataRowAction.Commit Then Exit Sub
If (Not ConfirmDelete) Then
e.Row.RejectChanges()
Exit Sub
End If

ConfirmDelete = False

Try
Dim s As String = "delete from tikshoret where [id]=" & e.Row("id",
DataRowVersion.Original)
Dim c As New SqlCommand(s, Conn)
c.ExecuteNonQuery()
e.Row.AcceptChanges()
Catch ex As Exception
e.Row.RejectChanges()
End Try

================

Now my q is if I go with C solution (see above) where I can catch this
exception.
In other word I can ask:
Where I can catch Exception that arise with built in control-responds?

Nov 21 '05 #9
You right I have mistake. I check it again, and I realize that this line is
not necessary:

Sub OnDel (ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
=>> If e.Action = DataRowAction.Commit Then Exit Sub

But you see that I divide my code between RowDeleted and RowDeleting.
At first I try to put all in RowDeleted. But the problem is, when MsgBox
appear,
the user not see the row that he start delete, and the grid stand on next
row.

I try fix it by calling RejectChange, and then after user confirm call
row.delete Within this event, this cause deadlock. Therefor, I divide code.

By the way, if you use RowChange Event to Update DB (that call to RowChange
again),
the first line on the event must:
If e.Action = DataRowAction.Commit then exit sub

Nov 21 '05 #10
> about a "remove" which is done by the combination of the del key.
which is: combination of the del key?
Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
3
by: Travis | last post by:
I am trying to prevent an exception from bubbling up from one nested Try/Catch Block to its "Parent" Try/Catch Block. Here is some example code: try{ try{ //Non-application killing code that...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
9
by: Mark Rae | last post by:
Hi, Can anyone please tell me if there is any difference in throwing an exception with or without parentheses, e.g. try { /* code */
5
by: Jim Bayers | last post by:
I have a simple page, all that's on it is a datagrid. The datagrid is populated on page_load when it is bound to a datareader. The problem is: when a user refreshes the page, new records are...
3
by: Ryan Liu | last post by:
Can someone give a sample to prevent a row from being deleted in a datatable? I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not work. I need verify if there other data...
7
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot...
10
by: amiga500 | last post by:
Hello, I have one basic simple question. When I have multiple records in the datagrid as follows: Code Product 1 Product 2 Product 3 11111 A B C...
1
by: usenet | last post by:
I wrote some sample code (see below) for nested exception throwing i.e. my catch blocks are throwing exceptions of their own (for simplicity I used standard exceptions). I am getting some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.