Solved

ASP.NET, How to handle Gridview foreign key constraints when deleting ??

Posted on 2011-03-07
8
1,208 Views
Last Modified: 2012-05-11
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)

Try

Catch ex As System.Data.SqlClient.SqlException

some 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 If

End Try

End Sub

Open in new window


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

0
Comment
Question by:SamJolly
  • 5
  • 2
8 Comments
 

Author Comment

by:SamJolly
Comment Utility
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

Open in new window


Thoughts?

Thanks,

Sam
0
 
LVL 1

Accepted Solution

by:
alagurajan_logica earned 500 total points
Comment Utility
Hi,

Insted of doing like this, you can get the current row reference from the GV and using the store procedure to delete the row from database, in the storeprocedure you can easily find is there any DB error is coming, if so use the output parameter to stop the process and display the error message.

Correct me if i am wrong

Rajan
0
 

Author Comment

by:SamJolly
Comment Utility
Hi Rajan,

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

Author Comment

by:SamJolly
Comment Utility
Sorted in SqlDataSource_Deleted event handler.

Thanks,

Sam
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:alagurajan_logica
Comment Utility
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
0
 

Author Comment

by:SamJolly
Comment Utility
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.

0
 
LVL 2

Expert Comment

by:alagurjan
Comment Utility
ok that is fine :)
0
 

Author Closing Comment

by:SamJolly
Comment Utility
thks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now