Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 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
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 20th, 2007, 05:38 AM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Why not using a unique constraint in ch 5?

Hi,

 In the [sprocCategoryInsertUpdateSingleItem] stored procedure there is a block of code:
IF (@id IS NULL) -- Insert a new item
BEGIN
  IF NOT EXISTS (SELECT Id FROM Category WHERE Description = @description AND ContentTypeId = @contentTypeId)
  BEGIN
    INSERT INTO Category(Description, ContentTypeId, SortOrder) VALUES(@description, @contentTypeId, @sortOrder)
    SET @returnValue = SCOPE_IDENTITY()
  END
  ELSE
  BEGIN
    SET @returnValue = -1 -- record already exists
  END
END

Why we don't use a unique constraint on the fields instead?

  #2 (permalink)  
Old September 20th, 2007, 06:53 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,

You could indeed use a unique constraint to have the database enforce uniqueness.

However, IMO, you would still need to do the check manually. Relying on the constraint results in exceptions being thrown when the item already exists. By manually checking the description you can avoid handling run-time exceptions when they aren't necessary....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #3 (permalink)  
Old September 20th, 2007, 09:25 AM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok :D

 


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
Insert Record Fails W/ Unique Constraint snw C# 0 September 2nd, 2005 02:14 PM
duplicate nulls in a column with unique constraint defiant SQL Server 2000 3 October 22nd, 2003 01:58 AM



All times are GMT -4. The time now is 04:49 AM.


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