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

  • SEARCH
  • Home
  • 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 7536989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:43:32+00:00 2026-05-30T06:43:32+00:00

here my problem, i’m using asp.net for my site, i get a list of

  • 0

here my problem, i’m using asp.net for my site, i get a list of books and show them, all is ok. now i try to sort this books by title, author or other attribute, i’m using this code:

public static IDataReader GetPageByCritere(
        int pageNumber,
        int pageSize,
        out int totalPages,
        string critere,
        string direction)
    {
        int pageLowerBound = (pageSize * pageNumber) - pageSize;
        totalPages = 1;
        int totalRows = GetCount();

        if (pageSize > 0) totalPages = totalRows / pageSize;

        if (totalRows <= pageSize)
        {
            totalPages = 1;
        }
        else
        {
            int remainder;
            Math.DivRem(totalRows, pageSize, out remainder);
            if (remainder > 0)
            {
                totalPages += 1;
            }
        }

        StringBuilder sqlCommand = new StringBuilder();
        sqlCommand.Append("SELECT * ");
        sqlCommand.Append("FROM a_book ");
        //sqlCommand.Append("WHERE  ");
        sqlCommand.Append("ORDER BY ?Critere ?direction ");
        sqlCommand.Append("LIMIT ?PageSize ");

        if (pageNumber > 1)
        {
            sqlCommand.Append("OFFSET ?OffsetRows ");
        }

        sqlCommand.Append(";");

        MySqlParameter[] arParams = new MySqlParameter[4];

        arParams[0] = new MySqlParameter("?PageSize", MySqlDbType.Int32);
        arParams[0].Direction = ParameterDirection.Input;
        arParams[0].Value = pageSize;

        arParams[1] = new MySqlParameter("?OffsetRows", MySqlDbType.Int32);
        arParams[1].Direction = ParameterDirection.Input;
        arParams[1].Value = pageLowerBound;

        arParams[2] = new MySqlParameter("?Critere", MySqlDbType.VarChar, 50);
        arParams[2].Direction = ParameterDirection.Input;
        arParams[2].Value = critere;

        arParams[3] = new MySqlParameter("?direction", MySqlDbType.VarChar, 50);
        arParams[3].Direction = ParameterDirection.Input;
        arParams[3].Value = direction;

        return MySqlHelper.ExecuteReader(
            GetReadConnectionString(),
            sqlCommand.ToString(),
            arParams);
    }
}

when i execute this code the books don’t get sorted, i get the first items list not sorted, here my sqlCommand and arParams for sorting with title for exemple :
sqlCommand

 {SELECT * FROM a_book ORDER BY ?Critere ?direction LIMIT ?PageSize ;}

arParams :

 {?PageSize} : 20 {?OffsetRows} : -20 {?Critere} title {?direction} DESC

please help, i didn’t find any solution.

  • 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-30T06:43:34+00:00Added an answer on May 30, 2026 at 6:43 am

    Unfortunately, this used to work in MySql but then the behaviour changed – see http://bugs.mysql.com/bug.php?id=31474. It now behaves like Oracle and SQL Server in this respect.

    Instead, you might want to consider building up your order by based on the criteria – hopefully, your critere string is not entered by a user or sourced from user input otherwise you’ll have to guard against SQL Injection.

    If your criteria consists of a single column, then you can simply do something like:

    sqlCommand.Append(String.Format("ORDER BY {0} {1}", Critere, direction))
    

    If Critere is multi columned then you might want to consider passing your sorting criteria as a list of column-direction.

    This is also covered here: Parameter in order by clause doesn't order -mysql, C#

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here the problem. I have my site hosted on a shared hosting for asp.net
I'm having a strange problem here... I have an ASP.NET 3.5 application that has
I've got a problem here with an MSI deployment that I'm working on (using
here is problem about sum of factorial of digit http://projecteuler.net/index.php?section=problems&id=254 also here is Define
Got a problem here on exiting onclick. The program uses shared preferences to get
I have problem here, I need all the stuffs inside this $_SESSION['cart'] thing and
Small problem here I want to make a small fb app which show different
Here's the problem: In C# I'm getting information from a legacy ACCESS database. .NET
Here is problem I'm trying to solve. It's for logistics company. I got all
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the

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.