473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How use dropdown to fill ORDER BY of SqlData Source

I am trying to use a dropdownlist to fill in the ORDER BY of a
SqlDataSource. Here's part of my HTML...

<asp:SqlDataSource ID="VideoList" runat="server" ConnectionString="<%$
ConnectionStrings:MyMainDatabase %>"
SelectCommand="SELECT aspnet_Users.UserName AS Username,
VideoCategory.VideoCategory, Videos.VideoId AS VideoId,
Videos.VideoDescription, Videos.VideoCaption, Videos.VideoPathUrl,
Videos.IsRacerProfileVideo, Videos.VideoCategoryId, Videos.TimesViewed AS
TimesViewed FROM Videos INNER JOIN VideoCategory ON Videos.VideoCategoryId =
VideoCategory.VideoCategoryId INNER JOIN aspnet_Users ON Videos.UserId =
aspnet_Users.UserId WHERE (Videos.IsRacerProfileVideo = 0) AND
(Videos.VideoCategoryId = @VideoCategoryId) ORDER BY @SortBy">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
Name="VideoCategoryId" PropertyName="SelectedValue" DefaultValue="" />
<asp:ControlParameter ControlID="ddlSort" DefaultValue=""
Name="SortBy" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

The dropdownlist is as follows:
<asp:DropDownList ID="ddlSort" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True" Value="TimesViewed">Most
Viewed</asp:ListItem>
<asp:ListItem Value="VideoId">Newest</asp:ListItem>
<asp:ListItem Value="Username">Username</asp:ListItem>
</asp:DropDownList>

Any ideas why this isn't working? Here's the error I'm getting...
The SELECT item identified by the ORDER BY number 1 contains a variable as
part of the expression identifying a column position. Variables are only
allowed when ordering by an expression referencing a column name.
Jan 26 '06 #1
4 1950
CK
SqlServer does not like variables in the ORDER BY Clause
ReWrite your SQL using a case statement similiar to this

DECLARE @SortBy varchar(30)
SET @SortBy = 'SomeColumn' *****I think you already decalre this in your
control

SELECT * FROM SomeTable
ORDER BY
CASE @SortBy
WHEN 'SomeColumn' THEN SomeColumn
WHEN 'OtherColumn' THEN OtherColumn
WHEN 'xColumn' THEN xColumn
END

HTH,
CK

"VB Programmer" <do**@emailme.com> wrote in message
news:Ou**************@TK2MSFTNGP11.phx.gbl...
I am trying to use a dropdownlist to fill in the ORDER BY of a
SqlDataSource. Here's part of my HTML...

<asp:SqlDataSource ID="VideoList" runat="server" ConnectionString="<%$
ConnectionStrings:MyMainDatabase %>"
SelectCommand="SELECT aspnet_Users.UserName AS Username,
VideoCategory.VideoCategory, Videos.VideoId AS VideoId,
Videos.VideoDescription, Videos.VideoCaption, Videos.VideoPathUrl,
Videos.IsRacerProfileVideo, Videos.VideoCategoryId, Videos.TimesViewed AS
TimesViewed FROM Videos INNER JOIN VideoCategory ON Videos.VideoCategoryId
= VideoCategory.VideoCategoryId INNER JOIN aspnet_Users ON Videos.UserId =
aspnet_Users.UserId WHERE (Videos.IsRacerProfileVideo = 0) AND
(Videos.VideoCategoryId = @VideoCategoryId) ORDER BY @SortBy">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
Name="VideoCategoryId" PropertyName="SelectedValue" DefaultValue="" />
<asp:ControlParameter ControlID="ddlSort" DefaultValue=""
Name="SortBy" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

The dropdownlist is as follows:
<asp:DropDownList ID="ddlSort" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True" Value="TimesViewed">Most
Viewed</asp:ListItem>
<asp:ListItem Value="VideoId">Newest</asp:ListItem>
<asp:ListItem Value="Username">Username</asp:ListItem>
</asp:DropDownList>

Any ideas why this isn't working? Here's the error I'm getting...
The SELECT item identified by the ORDER BY number 1 contains a variable as
part of the expression identifying a column position. Variables are only
allowed when ordering by an expression referencing a column name.

Jan 26 '06 #2
Do I have to create a new Stored Proc for this? I can't seem to use this as
SQL in the SqlDataSource SELECT statement...

"CK" <c_**********@hotmail.com> wrote in message
news:_D******************@newssvr13.news.prodigy.c om...
SqlServer does not like variables in the ORDER BY Clause
ReWrite your SQL using a case statement similiar to this

DECLARE @SortBy varchar(30)
SET @SortBy = 'SomeColumn' *****I think you already decalre this in your
control

SELECT * FROM SomeTable
ORDER BY
CASE @SortBy
WHEN 'SomeColumn' THEN SomeColumn
WHEN 'OtherColumn' THEN OtherColumn
WHEN 'xColumn' THEN xColumn
END

HTH,
CK

"VB Programmer" <do**@emailme.com> wrote in message
news:Ou**************@TK2MSFTNGP11.phx.gbl...
I am trying to use a dropdownlist to fill in the ORDER BY of a
SqlDataSource. Here's part of my HTML...

<asp:SqlDataSource ID="VideoList" runat="server" ConnectionString="<%$
ConnectionStrings:MyMainDatabase %>"
SelectCommand="SELECT aspnet_Users.UserName AS Username,
VideoCategory.VideoCategory, Videos.VideoId AS VideoId,
Videos.VideoDescription, Videos.VideoCaption, Videos.VideoPathUrl,
Videos.IsRacerProfileVideo, Videos.VideoCategoryId, Videos.TimesViewed AS
TimesViewed FROM Videos INNER JOIN VideoCategory ON
Videos.VideoCategoryId = VideoCategory.VideoCategoryId INNER JOIN
aspnet_Users ON Videos.UserId = aspnet_Users.UserId WHERE
(Videos.IsRacerProfileVideo = 0) AND (Videos.VideoCategoryId =
@VideoCategoryId) ORDER BY @SortBy">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
Name="VideoCategoryId" PropertyName="SelectedValue" DefaultValue="" />
<asp:ControlParameter ControlID="ddlSort" DefaultValue=""
Name="SortBy" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

The dropdownlist is as follows:
<asp:DropDownList ID="ddlSort" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True" Value="TimesViewed">Most
Viewed</asp:ListItem>
<asp:ListItem Value="VideoId">Newest</asp:ListItem>
<asp:ListItem Value="Username">Username</asp:ListItem>
</asp:DropDownList>

Any ideas why this isn't working? Here's the error I'm getting...
The SELECT item identified by the ORDER BY number 1 contains a variable
as part of the expression identifying a column position. Variables are
only allowed when ordering by an expression referencing a column name.


Jan 26 '06 #3
Is there a way to change the SelectCommand SQL statement in code?

"CK" <c_**********@hotmail.com> wrote in message
news:_D******************@newssvr13.news.prodigy.c om...
SqlServer does not like variables in the ORDER BY Clause
ReWrite your SQL using a case statement similiar to this

DECLARE @SortBy varchar(30)
SET @SortBy = 'SomeColumn' *****I think you already decalre this in your
control

SELECT * FROM SomeTable
ORDER BY
CASE @SortBy
WHEN 'SomeColumn' THEN SomeColumn
WHEN 'OtherColumn' THEN OtherColumn
WHEN 'xColumn' THEN xColumn
END

HTH,
CK

"VB Programmer" <do**@emailme.com> wrote in message
news:Ou**************@TK2MSFTNGP11.phx.gbl...
I am trying to use a dropdownlist to fill in the ORDER BY of a
SqlDataSource. Here's part of my HTML...

<asp:SqlDataSource ID="VideoList" runat="server" ConnectionString="<%$
ConnectionStrings:MyMainDatabase %>"
SelectCommand="SELECT aspnet_Users.UserName AS Username,
VideoCategory.VideoCategory, Videos.VideoId AS VideoId,
Videos.VideoDescription, Videos.VideoCaption, Videos.VideoPathUrl,
Videos.IsRacerProfileVideo, Videos.VideoCategoryId, Videos.TimesViewed AS
TimesViewed FROM Videos INNER JOIN VideoCategory ON
Videos.VideoCategoryId = VideoCategory.VideoCategoryId INNER JOIN
aspnet_Users ON Videos.UserId = aspnet_Users.UserId WHERE
(Videos.IsRacerProfileVideo = 0) AND (Videos.VideoCategoryId =
@VideoCategoryId) ORDER BY @SortBy">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
Name="VideoCategoryId" PropertyName="SelectedValue" DefaultValue="" />
<asp:ControlParameter ControlID="ddlSort" DefaultValue=""
Name="SortBy" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

The dropdownlist is as follows:
<asp:DropDownList ID="ddlSort" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True" Value="TimesViewed">Most
Viewed</asp:ListItem>
<asp:ListItem Value="VideoId">Newest</asp:ListItem>
<asp:ListItem Value="Username">Username</asp:ListItem>
</asp:DropDownList>

Any ideas why this isn't working? Here's the error I'm getting...
The SELECT item identified by the ORDER BY number 1 contains a variable
as part of the expression identifying a column position. Variables are
only allowed when ordering by an expression referencing a column name.


Jan 26 '06 #4
CK
See if that works for you.
I added the CASE clause to account for all the values in your drop down
list.

<asp:SqlDataSource ID="VideoList" runat="server" ConnectionString="<%$
ConnectionStrings:MyMainDatabase %>"
SelectCommand="SELECT aspnet_Users.UserName AS Username,
VideoCategory.VideoCategory, Videos.VideoId AS VideoId,
Videos.VideoDescription, Videos.VideoCaption, Videos.VideoPathUrl,
Videos.IsRacerProfileVideo, Videos.VideoCategoryId, Videos.TimesViewed AS
TimesViewed FROM Videos INNER JOIN VideoCategory ON
Videos.VideoCategoryId = VideoCategory.VideoCategoryId INNER JOIN
aspnet_Users ON Videos.UserId = aspnet_Users.UserId WHERE
(Videos.IsRacerProfileVideo = 0) AND (Videos.VideoCategoryId =
@VideoCategoryId) ORDER BY CASE @SortBy
WHEN 'Most Viewed' THEN TimesViewed
WHEN 'Newest' THEN VideoID
WHEN 'UserName' THEN UserName
END
">

CK

"VB Programmer" <do**@emailme.com> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl... Is there a way to change the SelectCommand SQL statement in code?

"CK" <c_**********@hotmail.com> wrote in message
news:_D******************@newssvr13.news.prodigy.c om...
SqlServer does not like variables in the ORDER BY Clause
ReWrite your SQL using a case statement similiar to this

DECLARE @SortBy varchar(30)
SET @SortBy = 'SomeColumn' *****I think you already decalre this in your
control

SELECT * FROM SomeTable
ORDER BY
CASE @SortBy
WHEN 'SomeColumn' THEN SomeColumn
WHEN 'OtherColumn' THEN OtherColumn
WHEN 'xColumn' THEN xColumn
END

HTH,
CK

"VB Programmer" <do**@emailme.com> wrote in message
news:Ou**************@TK2MSFTNGP11.phx.gbl...
I am trying to use a dropdownlist to fill in the ORDER BY of a
SqlDataSource. Here's part of my HTML...

<asp:SqlDataSource ID="VideoList" runat="server" ConnectionString="<%$
ConnectionStrings:MyMainDatabase %>"
SelectCommand="SELECT aspnet_Users.UserName AS Username,
VideoCategory.VideoCategory, Videos.VideoId AS VideoId,
Videos.VideoDescription, Videos.VideoCaption, Videos.VideoPathUrl,
Videos.IsRacerProfileVideo, Videos.VideoCategoryId, Videos.TimesViewed
AS TimesViewed FROM Videos INNER JOIN VideoCategory ON
Videos.VideoCategoryId = VideoCategory.VideoCategoryId INNER JOIN
aspnet_Users ON Videos.UserId = aspnet_Users.UserId WHERE
(Videos.IsRacerProfileVideo = 0) AND (Videos.VideoCategoryId =
@VideoCategoryId) ORDER BY @SortBy">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
Name="VideoCategoryId" PropertyName="SelectedValue" DefaultValue="" />
<asp:ControlParameter ControlID="ddlSort" DefaultValue=""
Name="SortBy" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

The dropdownlist is as follows:
<asp:DropDownList ID="ddlSort" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True" Value="TimesViewed">Most
Viewed</asp:ListItem>
<asp:ListItem Value="VideoId">Newest</asp:ListItem>
<asp:ListItem Value="Username">Username</asp:ListItem>
</asp:DropDownList>

Any ideas why this isn't working? Here's the error I'm getting...
The SELECT item identified by the ORDER BY number 1 contains a variable
as part of the expression identifying a column position. Variables are
only allowed when ordering by an expression referencing a column name.



Jan 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Charles | last post by:
I am trying to add the ability for a user to change the order in which the elements are listed in a dropdown list box. Before I added the ListID field the dropdown list box order was controlled...
7
by: Nicolae Fieraru | last post by:
Hi All, I am trying to change the rowsource of a combobox when I click on it. I played with many events, associated with the form and the combobox, but still haven't figured out what is the way...
6
by: Jenna Alten | last post by:
I have a datagrid with a template column that contains a dropdown list. I currently fill and display the dropdown list on the page load. This is working correctly. I am NOT using an Edit Column. I...
1
by: Mike C | last post by:
I'm new to Web development so I'm doing some bootstrapping and would really appreciate any help. Been grappling with this for days and am utterly confused by all the information on the web. I have...
19
by: Alex | last post by:
Hello list This question has probably already been asked, but let me ask again I have a mysql database to which I connect with my php scripts. The database contains articles. Name, Unit_Price...
2
by: Peter | last post by:
ASP.NET 2003 In the DataGrid how do I select current cell value in the dropdown box when I click on the edit link, currently when I click on the edit link in the DataGrid the dropdown box...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
2
by: yasodhai | last post by:
Hi, I used a dropdown control bind to a datagrid. <EditItemTemplate> <asp:DropDownList ID="FldType_edit" Runat="server" DataSource='< %#GetFieldType()%>' DataTextField="Type" />...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.