ASP.NET, How to handle Gridview foreign key constraints when deleting ??
Hi,
What is the beat way to intercept this SQLException, stop processing and display the error message when using the standard Gridview features. Currently the best I can see is by using something similar to:
Protected Sub gv_OnRowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)TryCatch ex As System.Data.SqlClient.SqlExceptionsome delete code here... Would expect a reference to the standard Gridview delete process somehow...If ex.Number = 547 Then 'handle foreign key violation(547)lblMsg.Text = "Record cannot be deleted as it is being used by other tables."End IfEnd TryEnd Sub
I guess there must be a very standard way of achieving this.
I am using ASP.NET2 and SQL Server 2005.
Thanks in advance,
Sam
ASP.NETVisual Basic.NETMicrosoft SQL Server 2005
Last Comment
SamJolly
8/22/2022 - Mon
SamJolly
ASKER
Further research produced this, but I get a stackoverflow error:
Protected Sub _gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles _gvFunds.RowDeleting Try _gvFunds.DeleteRow(e.RowIndex) (An unhandled exception of type 'System.StackOverflowException' occurred in System.dll error on this line) Catch ex As System.Data.SqlClient.SqlException If ex.Number = 547 Then 'handle foreign key violation(547) Me._lblError.Text = "record cannot be deleted as it is being used by another table." End If End Try End Sub
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
Yes I could imagine how one could use stored procedures for this. However there must be a standard approach to do this in ASP.NET by capturing the SQLException Error Number. Obviously I would like to understand this approach since I can use it for other errors.
Further thoughts?
Thanks.
SamJolly
ASKER
Sorted in SqlDataSource_Deleted event handler.
Thanks,
Sam
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
alagurajan_logica
Hi,
If you want to do it using Exception handling then what you have used in the coding is the correct way of handling, but in other thoughts it is not recommended way of doing this may cause performance problem to your application
Thanks
Rajan
SamJolly
ASKER
Ok Rajan, thanks for your help.
So in your view this error checking should be handled in the Stored Procedure?
For better or worse I am using a SQLDataSource at present which also contains the Delete DDL code. So perhaps if I need to put this back into a SP then I can also move the error code as well.
Open in new window
Thoughts?
Thanks,
Sam