Solved

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

Posted on 2011-03-07
8
1,244 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
ID: 35055875
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
ID: 35055995
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
ID: 35056126
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:SamJolly
ID: 35056494
Sorted in SqlDataSource_Deleted event handler.

Thanks,

Sam
0
 
LVL 1

Expert Comment

by:alagurajan_logica
ID: 35056663
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
ID: 35056862
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
ID: 35057007
ok that is fine :)
0
 

Author Closing Comment

by:SamJolly
ID: 35063654
thks
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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