Search code examples
c#percentagetableadapter

C# Percentage Calculations Not Calculating Properly


I'm developing a simple application which records data and then allows users to retrieve percentages based upon that data. Pass/Fail etc.

My Overall Calculations work just fine but when my 3,6,9 crate calculations take place they don't seem to change even though I've switched which user it should be pulling stats for.

I have changed my code to return a formatted string of the percent calculated in my calc_percent() method. I have also encapsulated my calculations in different methods to make it easier for me to read.

Here is a link to my github project. The project doesn't contain the dataset or tableadapters, I'm thinking my calculation errors could be related to my dataset queries but I'm not sure. I was under the impression that a Fill() method on a table adapter would fill the input dataset with the results of that query. This should allow me to preform further queries to narrow down my results correct? I may be using tableadapter queries improperly but I'm not sure.

#region Metrics Methods

private void generate_stats(User user)
{

    int crate_total = 0;

    try
    {
        crate_total = (int)this.CratesTableAdapter.count_crates_by_tech(user.Last_name);
        generate_overall_stats(user);

        // last 30 units stats
        if (crate_total >= 3)
        {
            generate_3_crate_stats(user);
        }

        // last 60 units stats
        if (crate_total >= 6)
        {
            generate_6_crate_stats(user);
        }

        // last 90 units stats
        if (crate_total >= 9)
        {
            generate_9_crate_stats(user);
        }

    }
    catch (NullReferenceException e)
    {
        MessageBox.Show(e.Message);
    }
    catch (OleDbException e)
    {
        MessageBox.Show(e.Message);
    }

}
private string calc_percent(int total, int number)
{
    double percentage = 0;

    percentage = ((double)number / total) * 100;

    string format_percent = string.Format("{0:0.00%}", percentage);
    try
    {
        if (percentage == 0)
        {
            throw new NullReferenceException("Calculation Has Failed, Possible Data Inaccuracy");
        }
    }catch(NullReferenceException e)
    {
        MessageBox.Show(e.Message);
    }
    return format_percent;
}
private void generate_overall_stats(User user)
{
    // Overall Stats
    int total = (int)this.Audit_ResultsTableAdapter.count_units_by_user(user.Last_name);
    int pass = (int)this.Audit_ResultsTableAdapter.count_pass_units_by_user(user.Last_name);
    int fail = total - pass;
    string ovr_pass_perc = calc_percent(total, pass);
    string ovr_fail_perc = calc_percent(total, fail);

    metrics_qc_overall_units_display.Text = total.ToString();
    metrics_qc_overall_pass_display.Text = ovr_pass_perc;
    metrics_qc_overall_fail_display.Text = ovr_fail_perc;
}
private void generate_3_crate_stats(User user)
{
    int crate_pass = 0;
    int crate_fail = 0;
    metrics_qc_last30_group.Visible = true;

    // Reset data set
    this.CratesTableAdapter.Fill(this.android_teamDataSet.Crates);
    // Get all crates by user in Desc order according to date (most recent dates at the top of the table)
    this.CratesTableAdapter.get_crates_by_tech(this.android_teamDataSet.Crates, user.Last_name);
    // Get the 3 most recent crates
    this.CratesTableAdapter.get_top_3_crates(this.android_teamDataSet.Crates);

    foreach (DataRow row in this.android_teamDataSet.Crates)
    {
        crate_pass = crate_pass + (int)row["passed_units"];
        crate_fail = crate_fail + (int)row["failed_units"];
    }

    int tmp_total = crate_pass + crate_fail;
    string pass_percent_30 = calc_percent(tmp_total, crate_pass);
    string fail_percent_30 = calc_percent(tmp_total, crate_fail);

    metrics_qc_last30_pass_display.Text = pass_percent_30;
    metrics_qc_last30_fail_display.Text = fail_percent_30;
}
private void generate_6_crate_stats(User user)
{
    int crate_pass = 0;
    int crate_fail = 0;

    metrics_qc_last60_group.Visible = true;

    this.CratesTableAdapter.Fill(this.android_teamDataSet.Crates);
    this.CratesTableAdapter.get_crates_by_tech(this.android_teamDataSet.Crates, user.Last_name);
    this.CratesTableAdapter.get_top_6_crates(this.android_teamDataSet.Crates);

    foreach (DataRow row in this.android_teamDataSet.Crates)
    {
        crate_pass = crate_pass + (int)row["passed_units"];
        crate_fail = crate_fail + (int)row["failed_units"];
    }

    int tmp_total = crate_pass + crate_fail;
    string pass_percent_60 = calc_percent(tmp_total, crate_pass);
    string fail_percent_60 = calc_percent(tmp_total, crate_fail);
    metrics_qc_last60_pass_display.Text = pass_percent_60;
    metrics_qc_last60_fail_display.Text = fail_percent_60;
}
private void generate_9_crate_stats(User user)
{
    int crate_pass = 0;
    int crate_fail = 0;

    metrics_qc_last90_group.Visible = true;
    this.CratesTableAdapter.Fill(this.android_teamDataSet.Crates);
    this.CratesTableAdapter.get_crates_by_tech(this.android_teamDataSet.Crates, user.Last_name);
    this.CratesTableAdapter.get_top_9_crates(this.android_teamDataSet.Crates);

    foreach (DataRow row in this.android_teamDataSet.Crates)
    {
        crate_pass = crate_pass + (int)row["passed_units"];
        crate_fail = crate_fail + (int)row["failed_units"];
    }

    int tmp_total = crate_pass + crate_fail;
    string pass_percent_90 = calc_percent(tmp_total, crate_pass);
    string fail_percent_90 = calc_percent(tmp_total, crate_fail);
    metrics_qc_last90_pass_display.Text = pass_percent_90;
    metrics_qc_last90_fail_display.Text = fail_percent_90;

}
private void set_users_metrics_defaults()
{

    metrics_qc_last30_group.Visible = false;
    metrics_qc_last60_group.Visible = false;
    metrics_qc_last90_group.Visible = false;


}

#endregion

#region Metrics Event Handlers

private void metrics_qc_display_panel_Paint(object sender, PaintEventArgs e)
{

}
private void metrics_auditor_combobox_SelectionChangeCommitted(object sender, EventArgs e)
{

}
private void metrics_qc_combobox_SelectionChangeCommitted(object sender, EventArgs e)
{
    set_users_metrics_defaults();
    User metrics_user = gen_user_for_metrics();
    generate_stats(metrics_user);
}

#endregion

Solution

  • As it stands any crate_total >9 will also evaluate for > 3 and > 6.

    if (crate_total >= 3)
    if (crate_total >= 6)
    if (crate_total >= 9)
    

    You'll need to put upper and lower limits in your if statement.

    // last 30 units stats
    if (crate_total >= 3 && crate_total < 6)
    
    // last 60 units stats
    if (crate_total >= 6 && crate_total < 9)
    
    // last 90 units stats
    if (crate_total >= 9)