Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6047093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:18:48+00:00 2026-05-23T07:18:48+00:00

I am trying to move all of my references to variables in SQL statements

  • 0

I am trying to move all of my references to variables in SQL statements to the SqlParameter class however for some reason this query fails.

string orderBy = Request.QueryString["OrderBy"];
//Fix up the get vars
if (orderBy == null)
    orderBy = "name ASC";

string selectCommand = "SELECT cat_id AS id, cat_name AS name FROM table_name ORDER BY @OrderBy";
SqlCommand cmd = new SqlCommand(selectCommand, dataConnection);
cmd.Parameters.Add(new SqlParameter("@OrderBy", orderBy));

//Create the SQLDataAdapter instance
SqlDataAdapter dataCommand = new SqlDataAdapter(cmd);

//Create the DataSet instance
DataSet ds = new DataSet();
//Get data from a server and fill the DataSet  
dataCommand.Fill(ds);

Here is the error

System.Data.SqlClient.SqlException: 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.

It fails on this line.

dataCommand.Fill(ds);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:18:49+00:00Added an answer on May 23, 2026 at 7:18 am

    You really have three options.

    1) Use a dataview to order the result set

    2) If you know the columns that can be ordered you can test for the string and then use then select the order. e.g.

    For example this will work

    DECLARE @orderby varchar(255)
    SET @orderby = 'Name ASC'
    
    SELECT [Your Column here ]FROM sys.tables 
    ORDER BY    
       case WHEN @orderby = 'Name ASC' Then name ELSE null END ASC,
       case WHEN @orderby = 'Name DESC' Then name ELSE null END DESC,
       CASE WHEN @orderby = 'Object_id ASC' then object_id ELSE null END ASC,
       CASE WHEN @orderby = 'Object_id DESC' then object_id ELSE null END DESC
    

    3) The final option is to do the same as #2 but in your C# code. Just be sure you don’t just tack on the ORDER BY clause from user input because that will be vunerable to SQL injection.

    This is safe because the OrderBy Url parameter "Name Desc; DROP table Users"will simply be ignored

    string SafeOrderBy = "";
    string orderBy = Request.QueryString["OrderBy"];
    //Fix up the get vars
    if (orderBy == null)
        orderBy = "name ASC";
    
    if (orderby == "name Desc")
    {
         SafeOrderBy == "name Desc"
    }
    
    
    string selectCommand = "SELECT cat_id AS id, cat_name AS name FROM table_name ORDER BY "
    selectCommand  += SafeOrderBy ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to move some divs using hover, this is my markup: <div class=item dark-grey>
I'm trying to move all the images stored in web application folder to a
I'm trying to move some Excel-Data to MySQL, but having troubles with encoding. What
I'm trying to move a control from one parent to another (if this will
I'm trying to model a specialization/generalization, leaning towards using class table inheritance (see this
I'm trying move a window partially off the top of the screen. However, Windows'
I am brand new to AS3 and I am trying to move all of
I'm trying to move the menu structure from a JMenu into a JMenuBar. The
I'm trying to move a MySQL database from a Linux machine to one running
I'm trying to move the data from one table to another based on names

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.