I am totally stuck trying to update my database with a simple on page calculation. The database column named Score remains at 0 and is never updated.
The code below runs fine by checking 4 questions for the correct answer, then displaying the total correct with
@totalMessage and the score as a percentage with
@Score%. It then redirects to another page that simply queries the database and displays it again.
My problem is I need to update my database with Score and for some reason, cannot seem to get that done with this form. I have no problem inserting or updating data so far, but cannot seem to get this new simple math variable to update.
I'm also trying to update the database with a hidden field, but so far nothing works.
Please help!
Code:
<form action="" method="post">
@{
var answ1 = PageData["Q1"];
var quest1C = "";
var answ2 = PageData["Q2"];
var quest2C = "";
var answ3 = PageData["Q3"];
var quest3C = "";
var answ4 = PageData["Q4"];
var quest4C = "";
if (answ1 == "B") {
quest1C = "1";
}
if (answ2 == "C") {
quest2C = "1";
}
if (answ3 == "C") {
quest3C = "1";
}
if (answ4 == "B") {
quest4C = "1";
}
var TotalCorrect = 0;
var Score = 0;
var totalMessage = "";
TotalCorrect = quest1C.AsInt() + quest2C.AsInt() + quest3C.AsInt() + quest4C.AsInt();
totalMessage = "Total Correct: " + TotalCorrect;
Score = (100 * TotalCorrect/4);
if(IsPost) {
var Id=Request["Id"];
var db = Database.Open("RC_ABG");
var sql = "";
sql = "UPDATE Gases SET Score = @0 WHERE Id = @1";
db.Execute(sql, Request["Score"], Id);
Response.Redirect("ResultsScorePosted.cshtml?id=" + Id);
}else{
}
}
<br/>
<div id="topThanx">Thank You @PageData["FirstName"] for submitting this exam and your time!
<br/>Your test results are below...</div>
<div id="Score">
@totalMessage
<br/><text>Your Score: @Score%</text>
</div>
<br/>
<br/>
<table border="0" cellpadding="3">
<tr>
<td align="left" valign="middle">
<input type="hidden" value="Score"/>
<input type="submit" value="See Test Results" onClick="willReset=confirm('See Results?');return willReset;">
</td>
</tr>
</table>
</div>
</div>
</form>