Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 28th, 2008, 06:40 PM
Authorized User
 
Join Date: Aug 2008
Location: , , .
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!
Reply With Quote
  #2 (permalink)  
Old December 29th, 2008, 04:22 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #3 (permalink)  
Old December 29th, 2008, 08:56 AM
Authorized User
 
Join Date: Aug 2008
Location: , , .
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!
Reply With Quote
  #4 (permalink)  
Old December 29th, 2008, 09:03 AM
Authorized User
 
Join Date: Aug 2008
Location: , , .
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!!
Reply With Quote
  #5 (permalink)  
Old December 29th, 2008, 10:06 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:41 AM.


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