Wrox Programmer Forums
|
BOOK: Beginning ASP.NET Web Pages with WebMatrix
This is the forum to discuss the Wrox book Beginning ASP.NET Web Pages with WebMatrix by Mike Brind, Imar Spaanjaars ; ISBN: 978-1-1180-5048-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Web Pages with WebMatrix section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2012, 03:57 PM
Authorized User
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default Updating Database with Variable Defined on Page

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>
 
Old June 9th, 2012, 01:19 PM
Wrox Author
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

Looks to me like

Code:
<input type="hidden" value="Score"/>
should be

Code:
<input type="hidden" name="Score" value="@Score" />
The Following User Says Thank You to Mike Brind For This Useful Post:
jpjamie (June 9th, 2012)
 
Old June 9th, 2012, 06:36 PM
Authorized User
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default Thank You!

I seem to look for the more complex, when it comes to coding and overlook the simpler. Will try this Monday and see how it goes. Thanks a lot Mike!! I REALLY want this code to work for me as it will be a template for many others. CHEERS! JJ
 
Old June 9th, 2012, 06:47 PM
Registered User
 
Join Date: May 2012
Posts: 14
Thanks: 3
Thanked 1 Time in 1 Post
Default

Which book is this example from?
 
Old June 10th, 2012, 01:57 PM
Authorized User
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by dejohnny View Post
Which book is this example from?
Sorry dejohnny.
Not related to any book example and I sense your subtle point.
I won't do it again...I promise!

JJ
 
Old June 11th, 2012, 09:40 AM
Authorized User
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default Thanks Again MIKE!

Quote:
Originally Posted by Mike Brind View Post
Looks to me like

Code:
<input type="hidden" value="Score"/>
should be

Code:
<input type="hidden" name="Score" value="@Score" />

You made my week (since this is Monday!).
I had a few other issues to clear up like defining the database variable, but I can now update the database for the score. As usual, it's all in the details.

JJ





Similar Threads
Thread Thread Starter Forum Replies Last Post
The variable or parameter "isdocument' is either not defined or it is out of scope JDarling XSLT 6 June 6th, 2011 07:00 AM
variable not defined error? SKhna ASP.NET 2.0 Basics 1 April 1st, 2008 05:48 AM
Setting variable to user defined type mantle51 SQL Server 2000 3 August 10th, 2007 03:52 AM
Compile Error: Variable not defined Fin Excel VBA 0 February 1st, 2006 01:45 PM
Compile error-variable not defined tmiller1 VB Databases Basics 1 February 15th, 2005 04:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.