Share via

Aspx how to order using Request.QueryString

Anonymous
2021-10-28T03:36:41.827+00:00

I have a music database running on SQL. I've never been able been able to order my queries by year (the logical choice). I have mutliple tables on the one page (albums, singles etc)

If someone can resolve my code for one I can apply to all:

e.g. objCmdLiveAlbums = New SqlDataAdapter("SELECT Year, Artist_Code, Artist, Title, Album_Code, Tracks FROM tbl_Artist_Albums WHERE (Length=4 OR Length=5) AND Collection = 0 AND Bootleg = 0 AND Live = 1 AND Artist_Code=" +
Request.QueryString("id"), objConn)

Because the order by always comes at the end of a SQL statement (ie after the WHERE) I can't order.

Any help appreciated

Cheers n Beers

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

Developer technologies | ASP.NET Core | Other
0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2021-10-28T07:24:14.593+00:00

Did you also try a statement like this?

objCmdLiveAlbums = New SqlDataAdapter("SELECT . . . AND Artist_Code=" &  
                                      Request.QueryString("id") & " ORDER BY Year DESC", objConn)  

By the way, to protect your site against attacks, it is recommended to avoid such string concatenations. Consider parameters. Check some examples: https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldataadapter.selectcommand.

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,106 Reputation points
    2021-10-28T07:54:16.19+00:00

    Hi @Anonymous ,
    Don't you do like this:

    "select Id,Text from Name where Text='"+ Request.QueryString["name"]+"' order by Id"  
    

    You only need add order by at the last.
    You need note the "" and ''.

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.