I'm trying to use a form to gather data and store them in a table and immediately do some calculations on that data and store the results in a separate table.. but I don't seem to be able to read the first table immediately after insertion.. I always encounter a zero in my calculation meaning that the first table is considered empty when trying to calculate (but is not empty when the page is refreshed, meaning that the data finally finds its way to the first table anyway) .. this is the example:

@{
// this is the code that gathers data from a form and stores the data in a table
 foreach(var row in db.Query(selectquestions))
 {
    var questionid = row.QuestionId;
    var answer = Request["Q" + row.QuestionId.ToString()];
    var insertanswers = "INSERT into Answers (UserId,QuestionId,Answer,ExamId) VALUES(@0, @1, @2, @3)";
    db.Execute(insertanswers,userid,questionid,answer,1);
  }

//this is the code that tries to immediately use the data stored by the above statement and store the new calculation results in a separate table

    int totalquestions= db.Query( some query using the first table ).Count();
    int correctanswers= db.Query( some query using the first table ).Count();
    int incorrectanswers=db.Query( some query using the first table ).Count();
    var score= (float)(correctanswers * 3 - incorrectanswers) / (totalquestions * 3)*100;

    var insertscores = "INSERT into Scores (UserId,ExamId,ItemId,Score) VALUES(@0, @1, @2, @3)"; 
    db.Execute(insertscores,userid,1,1,score.ToString());
}

I'm Using web pages/razor/sqlCE .. any advice would be appreciated

Related posts

Recent Viewed