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

M

mttc

I read articles that suggest preventing delete by throwing Exception from
RowDeleting Event.
I not understand where I can catch this Error?
 
C

Cor Ligthert

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
 
C

Cor Ligthert

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
 
M

mttc

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?
 
G

Guest

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.
 
K

Ken Tucker [MVP]

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/d...clientsqldataadapterclassrowupdatingtopic.asp

http://msdn.microsoft.com/library/d...ledboledbdataadapterclassrowupdatingtopic.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

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

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?
 
M

mttc

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
 
M

mttc

about a "remove" which is done by the combination of the del key.
which is: combination of the del key?
 

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

Top