user2112439
user2112439

Reputation:

Count how many times 'male' and 'female' appears in a field in access database (asp.net, C#)

I would like my asp.net application to count how times 'male' and 'female' appear in a certain field upon button click. This is what I have so far and would like the values to return in a label. How can I return those values in separate labels? One for Male and one for Female.

protected void btnRetrieve_Click(object sender, EventArgs e)
{
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=accessdatabase.mdb";
string cmdstr = "SELECT Gender FROM StudentList WHERE (GENDER = 'Male') OR (GENDER = 'Female')";

OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
MaleLabel = ????

}

Upvotes: 0

Views: 1476

Answers (1)

ThatBlairGuy
ThatBlairGuy

Reputation: 2462

Change your query to

select gender, count(gender)
from StudentList
group by gender

At that point you can retrieve the gender and the count directly from the results.

Upvotes: 2

Related Questions