Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Below, I attached coding and Screen shot, i get print preview output red block of 321 order but, i want 123 red block order. kindly solve my problem 1.NAME Block 2.AGE Block 3.ADDRESS Block,

         dAdapter = new OleDbDataAdapter
         ("SELECT TabTitle,A.ButtonName,A.CheckBoxName,B.SubCheckboxName 
           FROM (Select * from PersonalTrainerCheckboxData 
           where Checked=true and ButtonName<>'Startseite' and ButtonName<>'Ende'
          A LEFT JOIN 
          (Select * From PersonalTrainerSubcheckboxData where SubCheckBoxChecked=true  
          ORDER BY TabID ASC) 
          B ON 
          A.ButtonName = B.ButtonName AND A.TabID = B.TabID And 
          A.CheckBoxName = B.CheckBoxName 
          ORDER BY A.ButID ASC", connParam);

         cBuilder = new OleDbCommandBuilder(dAdapter);
         dataTable = new DataTable();
         ds = new DataSet();

         for (int i = 0; i < dataTable.Rows.Count; i++)
             createReport.AppendLine(dataTable.Rows[i][0].ToString() + " : " + dataTable.Rows[i][1].ToString() + ", " + dataTable.Rows[i][2].ToString()+ " - " + dataTable.Rows[i][3].ToString());


enter image description here

share|improve this question
Why are you using a data table instead of simply looping through a data reader? – Aaron Bertrand Jun 25 '14 at 11:05
Can you show us what PersonalTrainerCheckboxData.ButID is? I'm guessing it is "Address", "Age", and "Name" for this data set output. In which case that is what you're sorting on, explaining why the rows are ordered like this. – Thomas Stringer Jun 25 '14 at 12:15
@AaronBertrand I see no problem with that. As long as the memory footprint of the desired data isn't large (and it's typically not, in my experience) I default to a DataAdapter as opposed to a DataReader. I prefer my connections to be short-lived. In my opinion, 1) Get data then 2) process data. Those shouldn't be mixed. Again, just my opinion. There are other times when a DataReader is best/required, though. – Thomas Stringer Jun 25 '14 at 12:17
@ThomasStringer but if order is important, what is the point of populating the DataTable using an ORDER BY (which may as well not be there) but then looping through using just a for i loop handler? This is like throwing ORDER BY in a view and then expecting SELECT * from that view - without ORDER BY - to honor the ORDER BY saved in the view. – Aaron Bertrand Jun 25 '14 at 13:12
@AaronBertrand If order is required, you can either sort in the backend (T-SQL order by) or the front end (consuming code/application). I typically choose the former. The OP's outer ORDER BY is indeed effective. Looping through that result set with either a for or foreach loop will sequentially go through the data in the same order as retrieved. – Thomas Stringer Jun 25 '14 at 13:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.