Jump to content

Need help finding the error in this SQL statement

var select = "SELECT SenderID,Subject FROM Mail where UserID='Admin1' ORDER BY ID DESC";

i want to display some data from the database in a datagridview  in descending order. i can select the data and show it but when i add the ORDER BY ID DESC to the query it doesnt work.i think my query is wrong.what could really be the issue.

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, madknight3 said:

Is "ID" a column in the "Mail" table?

Oh thank you for showing it. it should be UserID.changing it to UserID did show an out put in the datagridview but it was not in descending order.any ideas what is wrong?  

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, Shammikit said:

Oh thank you for showing it. it should be UserID.changing it to UserID did show an out put in the datagridview but it was not in descending order.any ideas what is wrong?  

3

It's hard to know exactly what's wrong without seeing your data. The best way to know for sure is for you to post some test data and the correct order you want the data to be displayed. I'll give a couple guesses below though.

 

It could be that what you really want is to ORDER BY SenderID and not UserID. Example.

Given this data

SenderID, Subject, UserID
-------------------------
1, "Subject 1", Scott
2, "Subject 2", Admin
3, "Subject 3", Betty

Using ORDER BY UserID will sort it like this

2, "Subject 2", Admin
3, "Subject 3", Betty
1, "Subject 1", Scott

Since you aren't selecting UserID it could be that it just looks out of order.

2, "Subject 2"
3, "Subject 3"
1, "Subject 1"

Using ORDER BY SenderID will return this

1, "Subject 1", Scott
2, "Subject 2", Admin
3, "Subject 3", Betty

Or it could be that because you're using a string and not a number for your UserID column, it's sorting lexicographically and it's not doing what you want. Example.

Say you have this data

UserId
------
Admin1
Admin2
Admin100

You might think "ORDER BY UserID" will result in the above order. However, it will actually result in this order.

Admin1
Admin100
Admin2

Because the database doesn't look at the numbers at the end of the UserID as an integer.

 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

If this still isn't resolved, look at your datagridviews sorting properties. It might be that your grid is re-sorting the data after you bind the data to it.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×