Wrox Programmer Forums
|
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 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 February 11th, 2009, 08:41 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I'm going to try ONE MORE TIME to get this forum to accept the formatting I want to use!!!

Oh, sorry! I misread your code, somewhat. I thought you were going to always use your Get_CommentCount to get the counts, one blog entry at a time.

I see now that you just have made the mistake of not trusting SQL Server to do the right thing.

You can GET RID of your Get_CommentCount funtion, entirely, and rewrite the other function thus:
Code:
Public Function Increment_CommentCount(ByVal blogEntryId As Integer) As Integer
    Return CINT( SqlHelper.ExecuteScalar( _
                     _constring, _
                     CommandType.Text, _
                     "Update BlogEntry set commentcount=commentcount+1 where id= @blogEntryId; " & _
                     "SELECT commentcount WHERE id=@blogEntryId;", _
                     New SqlParameter("@blogEntryId", blogEntryId), _
                     New SqlParameter("@blogEntryId", blogEntryId) _
                ) )
End Function
But better, of course, would be to create a simple SP to do that:
Code:
Create Procedure IncrementCommentCount( @id INT )
AS
    SET NOCOUNT ON -- probably don't need this...won't hurt
    UPDATE BlogEntry set commentcount=commentcount+1 where id = @id
    SET NOCOUNT OFF
    SELECT commentcount WHERE id=@id
And then your VB code is reduced to as simple as perhaps:
Code:
Public Function Increment_CommentCount(ByVal blogEntryId As Integer) As Integer
    Return CINT( SqlHelper.ExecuteScalar( _
                     _constring, _
                     CommandType.Text, _
                     "EXECUTE IncrementCommentCount " & blogEntryId _
                ) )
End Function
All untested and I'm writing in this little tiny textarea window, so forgive typos. But hopefully you get the idea.

And I *STILL* think you'd be better off incorporating the increment into a SP where you INSERT the comment into the comments table.
 
Old February 11th, 2009, 11:22 PM
Authorized User
 
Join Date: Apr 2008
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Default Wrox Blog - Stored Procedure Update - Count Comments

Thanks! It never occurred to me to do this way..simple and elegant!!
Just added the following statement at the end of the existing CommentInsert procedure..

UPDATE BlogEntry SET CommentCount = CommentCount+1 WHERE id=@blogEntryId

This is not exactly a trigger but in the same procedure with 2 SQL statements..
 
Old February 12th, 2009, 01:08 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

That's better than a trigger, in the eyes of most DB designers.

You use triggers to handle inserts and updates and deletes that might occur from one of MANY sources.

When you only have on point of insert (the SP whose fragment you showed there), a trigger is overkill.

There are enough problems associated with triggers that they are best left as a kind of "last resort."

Very glad you opted to do it this way. I don't think you'll regret it.
The Following User Says Thank You to Old Pedant For This Useful Post:
norman001 (February 12th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrox Blog: Viewing individual blog entries Tawanda BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 7 May 7th, 2007 12:06 PM
Count character value using Stored Procedure itsurshailesh SQL Language 1 November 14th, 2006 01:31 AM
Update Tables in Stored Procedure Talsiter SQL Server 2005 1 April 10th, 2006 03:32 PM
How to update diffgram through stored procedure rajjr SQL Server ASP 0 March 3rd, 2006 02:17 AM
Problems returning count in Stored Procedure planza SQL Language 1 December 21st, 2005 03:24 PM





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