Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 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 14th, 2010, 11:01 AM
Registered User
 
Join Date: Jan 2010
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default What is the problem with tbh_Forums_GetThreads Stored Procedure?

Hi,
In chapter 8 it says that there are no tbh_Forums_GetThreads and tbh_Forums_GetThreadsByForum stored procedures;

This is because they must support different ordering (LastPostDate, ReplyCount, and ViewCount), and that we we can't parameterize the ORDER BY clause together with the ROW_NUMBER()... OVER statement.

Can you please explain what is the problem exactly?
 
Old February 14th, 2010, 12:58 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

The problem is that in T-SQL, as stated in the book, you cannot pass in a parameter that can be consumed by an ORDER BY clause.

That means, to order the results based on various criteria, the command text has to be built on the fly so that the ORDER BY criteria can be changed dynamically.

If you look at the code behind, you will see a big long command text string there, that is dyanamically built in order to support ordering.

Since you are using this string to execute the query, there's no purpose in having stored procedures to do it.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old February 14th, 2010, 06:02 PM
Registered User
 
Join Date: Jan 2010
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I don't see why I can't pass column name as string parameter and direction order as int parameter to the SP, and there using CASE, build appropriate querie, something like that:

Code:
.
.
.
 ROW_NUMBER() OVER (ORDER BY 
  CASE WHEN @OrderByColumn='LastPostDate' AND @OrderDirection=0 THEN tbh_Posts.LastPostBy END ASC,
  CASE WHEN @OrderByColumn='ReplyCount' AND @OrderDirection=0 THEN tbh_Posts.ReplyCount END ASC,
.
.
.
 
Old February 14th, 2010, 06:23 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You might be able to do that, but that isn't really a parameterized query, is it?. Also, it places a lot of UI logic in your sproc which doesn't rightly belong there.

In my opinion, the author's approach is the right one here. Not saying that is the only way to do it though.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old February 14th, 2010, 07:00 PM
Registered User
 
Join Date: Jan 2010
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default

why it's not parameterized query?
the sort column is passed in a right way:
Code:
  cmd.Parameters.Add("@OrderByColumn", SqlDbType.NVarChar).Value = orderByColumn;
            cmd.Parameters.Add("@OrderDirection", SqlDbType.Int).Value = orderDirection;
and it's safe regarding sql injects. Isn't it?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with stored procedure analuz ADO.NET 1 July 18th, 2007 08:33 AM
Stored procedure problem akumarp2p Reporting Services 1 May 29th, 2007 01:25 AM
Problem in stored procedure hkec SQL Language 1 October 6th, 2006 02:29 PM
Stored procedure problem dkspivey SQL Language 2 February 6th, 2006 01:44 PM
Stored Procedure Problem brettdavis4 ASP.NET 1.0 and 1.1 Basics 7 November 3rd, 2003 09:46 PM





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