I'm basically new in coding, and I'm in uni, we have a small project to complete and I'm using OleDb to do it, as I have used it before, I'm trying to count the number of rows where a field is equal to a specific value, but I always get 0. To be more clear, I want statistics showing percentage of males/females who completed the survey(the program I'm making), currently I have 1 male and 2 females, so my string for all rows shows 3, which is correct, but when I want to count all the rows that contain females, it shows 0, so the total percentage is 0, and the textbox that's supposed to show the stat shows 0, here is the code:
private void Form2_Load(object sender, EventArgs e)
{
String connectionString;
connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\kubas\Desktop\MyProject\ProjectDB.accdb'";
using (OleDbConnection myConnection = new OleDbConnection(connectionString))
{
string Gender1 = "Female";
OleDbConnection con;
OleDbCommand cmd = new OleDbCommand();
OleDbCommand cmd1 = new OleDbCommand();
con = new OleDbConnection(connectionString);
con.Open();
cmd.CommandText = "SELECT Count(Gender) FROM Survey WHERE Gender = '" + Gender1 + "'";
cmd1.CommandText = "SELECT Count(Gender) FROM Survey";
cmd.Connection = con;
cmd1.Connection = con;
con.Close();
try
{
con.Open();
int total = (Int32)cmd.ExecuteScalar();
int total1 = (Int32)cmd1.ExecuteScalar();
int tot;
tot = total / total1 * 100;
string ttal = Convert.ToString(tot);
textBox1.Text = ttal;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
sorry if there is something you can't understand, English isn't my first language.