I'm calculating the decimal netscore
multiple times during an application session and I'd like to compare the variable each time it changes in order to identify the three largest and three smallest values.
I'm currently storing the three largest & three smallest values in the session state. The following code doesn't always work correctly - for instance, if I set netscore
to the following values in this order: 57,64,27,56,45,53,62,42,64,40,53,71,57,54,50
it will return 71,71,64
as the three largest. I expect to see 71,64,64
.
Can someone identify what I'm doing wrong?
Session["top1"] = "0";
Session["top2"] = "0";
Session["top3"] = "0";
Session["bottom1"] = "100";
Session["bottom2"] = "100";
Session["bottom3"] = "100";
//Fetch values required to calculate netscore
SqlCommand fivecmd = new SqlCommand(query5, mySLTConnection);
var fives = Convert.ToSingle(fivecmd.ExecuteScalar());
SqlCommand fourcmd = new SqlCommand(query4, mySLTConnection);
var fours = Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand threecmd = new SqlCommand(query3, mySLTConnection);
var threes = Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand twocmd = new SqlCommand(query2, mySLTConnection);
var twos = Convert.ToSingle(twocmd.ExecuteScalar());
SqlCommand onecmd = new SqlCommand(query1, mySLTConnection);
var ones = Convert.ToSingle(onecmd.ExecuteScalar());
mySLTConnection.Close();
//Get total count
var total = fives + fours + threes + twos + ones;
//Get net score
var netscore = Convert.ToDecimal((((fives + fours) - (twos + ones)) / total)*100);
netscore = Math.Round(netscore,0);
//Begin comparing netscore to stored top/bottom values
if (netscore > Convert.ToDecimal(Session["top1"]))
{
Session["top3"] = Session["top2"];
Session["top2"] = Session["top1"];
Session["top1"] = netscore;
Session["top1q"] = question.ToUpper();
}
else if (netscore > Convert.ToDecimal(Session["top2"]))
{
Session["top3"] = Session["top2"];
Session["top2"] = netscore;
Session["top2q"] = question.ToUpper();
}
else if (netscore > Convert.ToDecimal(Session["top3"]))
{
Session["top3"] = netscore;
Session["top3q"] = question.ToUpper();
}
else if (netscore < Convert.ToDecimal(Session["bottom1"]))
{
Session["bottom3"] = Session["bottom2"];
Session["bottom2"] = Session["bottom1"];
Session["bottom1"] = netscore;
Session["bottom1q"] = question.ToUpper();
}
else if (netscore < Convert.ToDecimal(Session["bottom2"]))
{
Session["bottom3"] = Session["bottom2"];
Session["bottom2"] = netscore;
Session["bottom2q"] = question.ToUpper();
}
else if (netscore < Convert.ToDecimal(Session["bottom3"]))
{
Session["bottom3"] = netscore;
Session["bottom3q"] = question.ToUpper();
}
lblSVal1.Text = Session["top1"].ToString();
lblSVal2.Text = Session["top2"].ToString();
lblSVal3.Text = Session["top3"].ToString();