i'm helping a friend and i'm almost done with the program what i can't figure out is i need to sort the number of votes from lowest to highest my database is just ms access and i'm trying to display the photos of the candidates who got the highest votes but i'm so lost here i hope somebody can guide me.

Recommended Answers

To sort as part of your query, use an ORDER BY clause.

Does that help? If not, please post some code so we can get at specifics more easily.

Jump to Post

Ok. If that's the case, there's no need retrieve the record(s) with the highest number of votes.

You could use a DataAdapter instead of DataReader for your sqlcouncil query (and select all fields, not just num_of_votes), place the result into a DataTable, and retrieve the first 8 rows.

Jump to Post

First of all, unless you place all those pictureboxes in a list<of pictureboxes>, you cannot fill them using a loop; you're gonna have to fill them manually.

Secondly, there is no need for con.Open.

Jump to Post

All 11 Replies

To sort as part of your query, use an ORDER BY clause.

Does that help? If not, please post some code so we can get at specifics more easily.

ok i edited my post i dont know how to post a code

String ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\kirster\\Downloads\\Compressed\\Voting_Aguirre\\Voting_Aguirre\\database\\Database1.mdb;Persist Security Info=False";
                  OleDbConnection con = new OleDbConnection(ConStr);
                  String sqlcouncil = "select num_of_votes from tblCouncilors order by num_of_votes";
                 OleDbCommand cmdcouncil = new OleDbCommand(sqlcouncil , con);
                 OleDbDataAdapter dacouncil = new OleDbDataAdapter();
                 OleDbDataReader readercouncil;


                 readercouncil = cmdcouncil.ExecuteReader();
                 DataTable dtcouncil = new DataTable();
                 dtcouncil.Load(readercouncil);


                 for (int x = 0; x < dtcouncilRows.Count; x++ )
                 {
                     if(max < Int32.Parse(dtcouncil.Rows[x]["num_of_vote"].ToString()))
                         max = Int32.Parse(dtcouncil.Rows[x]["num_of_vote"].ToString());
                 }


                 String^ sqlselectcouncil = "select * from tblCouncilors where num_of_vote = " + max + "";

                 OleDbDataAdapter dacouncil1 = gcnew OleDbDataAdapter(sqlselectcouncil , con);
                 DataSet^ dscouncil;
                 dscouncil = gcnew DataSet("tblCouncilors");

                 dacouncil1.Fill(dscouncil , "tblCouncilors");

                 DataRow Drowcouncil = dscouncil.Tables[0].Rows[0];
                 piccouncil1.Image = Bitmap.FromFile(Drowcouncil[1].ToString());

Use desc with order by to get a descending table

select num_of_votes from tblCouncilors order by num_of_votes desc

thanx for that but my problem is still there i cant display the 8 winners its only displaying the one who got the very highest vote

Lines 1 to 11 gets table with all votes in descending order.. ok
Lines 14 to 18 basically retrieves the highest number of votes.. not really necessary, is it?
Lines 21 to 27 gets the record(s) with the highest number of votes, meaning it gets only 1 record unless there's a tie
Lines 29 to 30 retrieves the image of the first (and only if there's no tie) record retrieved

I'm confused. Where exactly do you intend to place images of winning candidates? How many images would be there to display? You've only 1 control that can display an image, namely piccouncil1.

i have 8 controls im just not adding it yet because it only display the highest vote and im still trying to figure out the logic behind this

it's on the tip of my head but i cant seem to do it i know i have to loop it

Ok. If that's the case, there's no need retrieve the record(s) with the highest number of votes.

You could use a DataAdapter instead of DataReader for your sqlcouncil query (and select all fields, not just num_of_votes), place the result into a DataTable, and retrieve the first 8 rows.

i dont know whats wrong but i did change the code to use dataTable

String ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\kirster\\Downloads\\Compressed\\Voting_Aguirre\\Voting_Aguirre\\database\\Database1.mdb;Persist Security Info=False";
             OleDbConnection^ con = gcnew OleDbConnection(ConStr);
             String sqlselectcouncil = "select * from tblCouncilors order by num_of_vote desc";

                 OleDbDataAdapter dacouncil1 = gcnew OleDbDataAdapter(sqlselectcouncil , con);
                 DataTable dt = new DataTable();

                 con.Open();
                 dacouncil1.Fill(dt);


                 for each(DataRow row in dt->Rows)
                 {
                      piccouncil1.Image = Bitmap.FromFile(row["image"].ToString());
                      piccouncil2.Image = Bitmap.FromFile(row["image"].ToString());     
                 }

First of all, unless you place all those pictureboxes in a list<of pictureboxes>, you cannot fill them using a loop; you're gonna have to fill them manually.

Secondly, there is no need for con.Open.

tnx now it's done i just need some polishing to do thnx alot for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.