Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
plz i try many thing but i can't sort my database in form im not good in asking i use this code to my from i want to sort column name ( answer ) to get the null call first

What I have tried:

C#
private void Interior_Load(object sender, EventArgs e)
       {



           string mainconn = ConfigurationManager.ConnectionStrings["MY"].ConnectionString;
           using (SqlConnection sqlconn = new SqlConnection(mainconn))
           {
               sqlconn.Open();

               using (SqlCommand sqlcomm = new SqlCommand("SELECT * FROM remaining WHERE username=@username", sqlconn))
               {
                   sqlcomm.Parameters.AddWithValue("@username", txtusername.Text);
                   using (SqlDataAdapter sda = new SqlDataAdapter(sqlcomm))
                   {

                       ds = new DataSet();
                       sda.Fill(ds);


                   }
               }
       }
     }
Posted
Updated 16-Feb-19 23:39pm

Easiest way?
Use an ORDER BY clause on your SQL:
using (SqlCommand sqlcomm = new SqlCommand("SELECT * FROM remaining WHERE username=@username ORDER BY MySortColumn DESC", sqlconn))


But ... tw things.
1) Don't use SELECT * FROM - always name the columns you want to retrieve in the order you want to retrieve them. That way your code or display doesn't get messed up by a future change to the DB, and you don't waste memory or bandwidth fetching data you aren't going to use.
2) Why would you have more than one user with a same username? Normally, the username uniquely identifies a user and having two with the same name complicates matters...
 
Share this answer
 
Comments
el_tot93 17-Feb-19 5:32am    
thx bro it work with me bout the last thing is how to sort it from z to a ( it now sort from a to z )
OriginalGriff 17-Feb-19 5:50am    
What did I say about thinking, some days ago?
Change DESC to ASC ...

Seriously, you'd save yourself a lot of time if you did ...
el_tot93 17-Feb-19 5:53am    
thx allot my bro you safe my life :D thx
el_tot93 17-Feb-19 5:36am    
1) i need all columns in the table
2) i need to get the columns with that user name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900