Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2008, 06:40 PM
Authorized User
 
Join Date: Aug 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reference Constraint?

You mention in your intro to databases chapter how to setup a diagram in the database to enforce not being able to delete (for example) a category record, if it's ID is used in a record in, say a Products table.

It works, but now when I write a GridView that deletes Category records, I get an ugly reference constraint asp.net error. Is there some way to code it in the ItemDeleting code or somewhere to cancel the delete and change a lable.text to "cannot delete category when it is still being referenced in products table" ?

Thoughts?
Thanks!
 
Old December 29th, 2008, 04:22 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Check out the TIO on page 494. It shows you how to disable the Delete link to stop users from deleting records that still have associated child records. Additionally, check out the TIO on page 498. This TIO shows you how to handle errors like the foreign key constraint exception you mentioned.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 29th, 2008, 08:56 AM
Authorized User
 
Join Date: Aug 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQLException is not defined?

Thanks Imar!!

I have about 100 pages of reading to finish up to get there (I am still just starting the LINQ chapter...)

But I am dealing with a client who has this need now. When I simply add the code on page 500 to my GridView:

Code:
ProtectedSub dsCategories_Deleted(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles dsCategories.Deleted
If e.Exception IsNotNothingAndAlsoTypeOf (e.Exception) Is sq SqlException Then
Dim myException As SqlException = CType(e.Exception, SqlException)
If myException.Number = 547 Then
Message.Text = "Sorry, you cannot delete this Category because " & _
"it has associated plans that you need to delete or " & _
"reassign first"
e.ExceptionHandled = True
EndIf
EndIf
EndSub
and try to load the page, I get the error: "Type 'SqlException' is not defined"

I am struggling to find time to finish my reading, while working on client projects ... any chance you could give me a quick hint of where I need to define the SQLException to help me limp along until I get my reading done?

Let me know - thanks again!
 
Old December 29th, 2008, 09:03 AM
Authorized User
 
Join Date: Aug 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Yeah... never mind! ;)

Forgot the:
Imports System.Data.SqlClient
at the top of my .vb codebehind - works like a champ now!


Even added a few other 'features' for my own use:
Code:
ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
Message.Text = "<font color=black>Edit Existing Categories...</font>"
EndSub
 
 
ProtectedSub dsCategories_Deleted(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles dsCategories.Deleted
If e.Exception IsNotNothingAndAlsoTypeOf (e.Exception) Is SqlException Then
Dim myException As SqlException = CType(e.Exception, SqlException)
If myException.Number = 547 Then
Message.Text = "Sorry, you cannot delete this Category because " & _
"it has associated plans that you need to delete or " & _
"reassign first"
e.ExceptionHandled = True
EndIf
Else
Message.Text = "Category deleted..."
EndIf
EndSub
 
 
ProtectedSub GridView1_RowUpdated(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView1.RowUpdated
Message.Text = "Category updated..."
EndSub

Thanks again Imar!!
 
Old December 29th, 2008, 10:06 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome.....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I have a constraint like this.....? callagga SQL Language 7 August 13th, 2008 01:08 AM
Constraint Vs Index carumuga SQL Server 2000 2 December 4th, 2007 02:14 AM
Why not using a unique constraint in ch 5? asptwodev BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 2 September 20th, 2007 09:25 AM
constraint for 1-to-n relation j_yan MySQL 0 December 10th, 2004 11:03 PM
Constraint for names barkingstars Oracle 2 April 14th, 2004 01:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.