Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Gridview SqlDataSource toggle between Ascending and Descding order using a control param

Posted on 2009-07-06
5
Medium Priority
?
474 Views
Last Modified: 2013-11-07
Am using a simple radiobutton to let the user toggle between ascending and descending order on a gridview. Already have a CASE WHEN THEN statement to let the user toggle between the sort key but the same approach does not seem to work on ASC or DESC.

See code for a clearer explanation. NB : code does not work
<asp:SqlDataSource ID="SqlDataSource_ContactHyperlink" runat="server" ConnectionString="<%$ ConnectionStrings:conn_db1 %>"
                                            ProviderName="System.Data.SqlClient" 
                                           SelectCommand="SELECT IndexID, OrderID, hyperlink_url, DomainName, 
                                            hyperlink_memo, origin_UserName, origin_UserID, origin_Visibility, origin_dateTime, starRating 
                                            FROM tbl_bookmarks 
                                            WHERE (origin_UserID = @curUserID) 
                                            ORDER BY CASE @SortBy 
                                            WHEN 'IndexID' THEN cast(IndexID AS varchar(256))
                                            WHEN 'starRating' THEN cast(starRating AS varchar(256))
                                            WHEN 'domain' THEN DomainName
                                            END
                                            CASE @SortOrder
                                            WHEN 'ascending' THEN ASC
                                            WHEN 'descending' THEN DESC
                                            END">
                                            <SelectParameters>
                                                <asp:SessionParameter Name="curUserID" SessionField="UserGUID" Type="String" />
                                                <asp:ControlParameter ControlID="DropDownList_sortOrder" Name="SortBy" PropertyName="SelectedValue" Type="String" />
                                                 <asp:ControlParameter ControlID="RadioButtonList_sortType" Name="SortOrder" PropertyName="SelectedValue" Type="String" />
                                            </SelectParameters>
                                        </asp:SqlDataSource>

Open in new window

0
Comment
Question by:JOHNFROG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24784744
You cannot achieve this is in a T-SQL statement and you need to use Dynamic SQL like below:

Hope this helps
Declare @sql nvarchar(1000);
set @sql = 'SELECT IndexID, OrderID, hyperlink_url, DomainName, 
hyperlink_memo, origin_UserName, origin_UserID, origin_Visibility, origin_dateTime, starRating 
FROM tbl_bookmarks 
WHERE (origin_UserID = ' + @curUserID + ') 
ORDER BY CASE @SortBy 
WHEN 'IndexID' THEN cast(IndexID AS varchar(256))
WHEN 'starRating' THEN cast(starRating AS varchar(256))
WHEN 'domain' THEN DomainName
END ' + (SELECT CASE @SortOrder WHEN 'ascending' THEN 'ASC' WHEN 'descending' THEN 'DESC' END);
EXEC sp_executesql @sql;

Open in new window

0
 

Author Comment

by:JOHNFROG
ID: 24784834
think I am losing something in my implementation as am getting an error

"Incorrect syntax near 'IndexID'

See code snippet my interpretation of what you have suggested
  <asp:SqlDataSource ID="SqlDataSource_ContactHyperlink" runat="server" ConnectionString="<%$ ConnectionStrings:conn_trymoreharder %>"
                                            ProviderName="System.Data.SqlClient" 
                                           SelectCommand="Declare @sql nvarchar(1000);
                                            set @sql = 'SELECT IndexID, OrderID, hyperlink_url, DomainName, 
                                            hyperlink_memo, origin_UserName, origin_UserID, origin_Visibility, origin_dateTime, starRating 
                                            FROM tbl_bookmarks 
                                            WHERE (origin_UserID = ' + @curUserID + ') 
                                            ORDER BY CASE @SortBy 
                                            WHEN 'IndexID' THEN cast(IndexID AS varchar(256))
                                            WHEN 'starRating' THEN cast(starRating AS varchar(256))
                                            WHEN 'domain' THEN DomainName
                                            END ' + (SELECT CASE @SortOrder WHEN 'ascending' THEN 'ASC' WHEN 'descending' THEN 'DESC' END);
                                            EXEC sp_executesql @sql;">
 
                                            <SelectParameters>
                                                <asp:SessionParameter Name="curUserID" SessionField="UserGUID" Type="String" />
                                                <asp:ControlParameter ControlID="DropDownList_sortOrder" Name="SortBy" PropertyName="SelectedValue" Type="String" />
                                                <asp:ControlParameter ControlID="RadioButtonList_sortType" Name="SortOrder" PropertyName="SelectedValue" Type="String" />
                                            </SelectParameters>
                                        </asp:SqlDataSource>

Open in new window

0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 2000 total points
ID: 24785056
Script which I gave you will work fine.
Just take care of single and double quotes to correct it out.
0
 

Author Closing Comment

by:JOHNFROG
ID: 31600131
genius
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24785132
Thanks..
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question