'-----------------------------------------------------------------------
' WORKING WITH SESSION STATE VARIABLES & Custom Error Pages
' C# 206 Class
' Created 10/16/05
'-----------------------------------------------------------------------
'This program lets users practice addition fact. The numbers they enter and the amt correct
'are stored in session variables so we can use them across round trips.
'The program tracks number of items correct, number of items given, and the percentage correct
'I use the C# random number generator to create the problems and use a range of 1-40 for the possible
'values.
'Session variables are like super global variables to the programmer because we can access them from anywhere
'in our application.
'Notes:
'EnableViewState=False for the entire doument because we are saving our data in session state variables.
'The web.config has been changed so that any error will show the default-error.htm page I created.
'Open web.config and look under Custom errors. Usually it says
'Notice I changed it to .
'I left the original in the commented section so you can use either one by commenting out the one
'you do not want.
Public Class WebForm1
Inherits System.Web.UI.Page
'---I need these from several places
Dim m_TotalItems As Integer
Dim m_TotalRight As Integer
Dim m_FirstNumber As Integer
Dim m_SecondNumber As Integer
Dim m_randomNumber As Random
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'---first time loading we start clean. Initialize Session variables
If Not IsPostBack Then
Session("m_TotalItems") = 0
Session("m_TotalRight") = 0
Session("FirstNumber") = 0
Session("SecondNumber") = 0
Shuffle() 'create the numbers for the problems
Else
'---they are in the process of doing probs so get the stuff that was on screen on the last
'http request. We grab data from our session variables and assign them
m_TotalItems = Session("m_TotalItems")
m_TotalRight = Session("m_TotalRight")
m_FirstNumber = Session("FirstNumber")
m_SecondNumber = Session("SecondNumber")
End If
RefreshDisplay() 'now show numbers
End Sub
Private Sub Shuffle()
Dim Min As Integer = 1
Dim Max As Integer = 40
'---get random numbers in the range of 1-40
m_randomNumber = New Random
m_FirstNumber = m_randomNumber.Next(Min, Max)
m_SecondNumber = m_randomNumber.Next(Min, Max)
'---now save these numbers
Session("FirstNumber") = m_FirstNumber
Session("SecondNumber") = m_SecondNumber
End Sub
Private Function Answer() As Integer
'---calculate answer
Answer = m_FirstNumber + m_SecondNumber
End Function
Private Sub RefreshDisplay()
lblFirst.Text = m_FirstNumber
lblSecond.Text = "+ " & m_SecondNumber
End Sub
Private Sub txtAnswer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAnswer.TextChanged
'---we showed them another prob so increment, save, & display it
m_TotalItems += 1
Session("m_TotalItems") = m_TotalItems
lblNumItems.Text = m_TotalItems
If txtAnswer.Text = Answer() Then
lblComment.Text = "Correct!"
'---now let's keep track of how many they get right
'---increment number of items & correct answers
Session("m_TotalRight") += 1
m_TotalRight += 1
'---show another prob
txtAnswer.Text = ""
Shuffle()
RefreshDisplay()
Else
txtAnswer.Text = ""
lblComment.Text = "Sorry, try again."
End If
'---now show how many they got right. Use the Session because they may have not gotten
'it right and we have to use the latest value.
lblCorrect.Text = CStr(Session("m_TotalRight"))
'---show percent correct
If m_TotalRight > 0 Then
lblPercent.Text = CInt(m_TotalRight / m_TotalItems * 100)
End If
End Sub
End Class