-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent.aspx.vb
209 lines (150 loc) · 7.46 KB
/
student.aspx.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class student
Inherits System.Web.UI.Page
'connection String
Private connectionString As String = WebConfigurationManager.ConnectionStrings("Quiz_DB").ConnectionString
Public Shared UserID As Integer
'loading all the user data
Private Sub student_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
If Session("UserRole") = "Student" Then
'take the user data from the from database And update the userprofile accordingly
UserID = Session("UserID")
Dim studentProfilelabel As New Label
StudentProfile.Controls.Clear()
studentProfilelabel.ID = "txtLabel_instructorProfile"
update_UserProfile(studentProfilelabel)
StudentProfile.Controls.Add(studentProfilelabel)
Else
Response.Redirect("AccessDenied.aspx")
End If
End If
End Sub
'Function for updating the userProfile from database
Private Sub update_UserProfile(label_UserProfile As Label)
label_UserProfile.Controls.Clear()
' Define ADO.NET objects.
Dim selectSQL As String
'general user data
selectSQL = "SELECT UserID, FullName, Address, Email, PhoneNumber FROM Quiz_User Where userID = " & UserID
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(selectSQL, con)
Dim adapter As New SqlDataAdapter(cmd)
Dim dsQuizDB As New DataSet()
' Try to open database and read information.
Try
con.Open()
adapter.Fill(dsQuizDB, "Quiz_Users")
'specific user data
cmd.CommandText = "SELECT StudentID, UserID, Marks, Semester, Department FROM Quiz_Student Where userID = " & UserID
adapter.Fill(dsQuizDB, "Quiz_students")
Catch err As Exception
label_UserProfile.Text = "Error 404! The User Account Doesn't found "
'label_UserProfile.Text &= err.Message
Finally
con.Close()
End Try
Try
'The first augument, defines the unique name for the relation, while the 2nd and 3rd shows
' the unique parent and child relation in the tables respectivley
'relation between quiz user and quiz students
Dim QuizUsers_QuizStudents As New DataRelation("QuizUsers_QuizStudents",
dsQuizDB.Tables("Quiz_Users").Columns("UserID"),
dsQuizDB.Tables("Quiz_Students").Columns("UserID"))
'add relations
dsQuizDB.Relations.Add(QuizUsers_QuizStudents)
Dim userRow As DataRow
Dim studentRow As DataRow
studentRow = dsQuizDB.Tables("Quiz_Students").Rows.Item(0)
userRow = studentRow.GetParentRow(QuizUsers_QuizStudents)
InsertLabel("Student Name: ", userRow("FullName"), "txtlabel_studentfullName")
InsertLabel("Student Address: ", userRow("Address"), "txtlabel_studentAdress")
InsertLabel("Student Email: ", userRow("Email"), "txtlabel_studentEmail")
InsertLabel("Student Phone Number: ", userRow("PhoneNumber"), "txtlabel_studentPhone")
InsertLabel("Student ID: ", studentRow("StudentID"), "txtlabel_studentID")
InsertLabel("Semester : ", studentRow("Semester"), "txtlabel_studentsemester")
InsertLabel("Department : ", studentRow("Department"), "txtlabel_studentdepartment")
'Update the students marks here
Session("StudentMarks") = studentRow("Marks")
Session("StudentID") = studentRow("StudentID")
Catch ex As Exception
showErrorMessages.Text = "<b>Sorry For inconvenience! There are issues with the Student Account!</b>"
btnShowResult.Visible = False
btnStartQuiz.Visible = False
title.Visible = False
End Try
End Sub
'Functions for dynamically inserting the label
Private Sub InsertLabel(labelText As String, labelValue As String, labelId As String)
Dim txtLabel As New Label
txtLabel.ID = labelId
txtLabel.Text = "<br/>" & labelText & "   " & labelValue & "<br/>"
StudentProfile.Controls.Add(txtLabel)
End Sub
Private Sub btnShowResult_Click(sender As Object, e As EventArgs) Handles btnShowResult.Click
Dim studentMarks As Integer = Session("StudentMarks")
If studentMarks.ToString IsNot Nothing Then
InsertLabel("Your best Score is: ", studentMarks.ToString(), "txtlabel_studentmarks")
Else
InsertLabel("You havn't take any Quiz Yet!", "", "txtlabelmarks")
End If
End Sub
Private Sub btnStartQuiz_Click(sender As Object, e As EventArgs) Handles btnStartQuiz.Click
If GetTotalNumberOfQuestionsToBeInserted().ToString IsNot Nothing And GetNumberOfQuestionsInserted().ToString IsNot Nothing Then
If (GetTotalNumberOfQuestionsToBeInserted() - GetNumberOfQuestionsInserted()) = 0 Then
Dim url As String
url = "Quiz.aspx"
Response.Redirect(url)
Else
Response.Redirect("AccessDenied.aspx")
End If
Else
Response.Redirect("AccessDenied.aspx")
End If
End Sub
Private Function GetTotalNumberOfQuestionsToBeInserted() As Integer
Dim SQLStatement As String
Dim NumberOfQuestionsToBeInserted As Integer
SQLStatement = "SELECT NoOfQuestions As NoOfQuestions from Quiz_Setting WHERE QuizSettingID = 1"
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(SQLStatement, con)
Dim adapter As New SqlDataAdapter(cmd)
Dim dsQuizDB As New DataSet()
' Try to open database and read information.
Try
con.Open()
adapter.Fill(dsQuizDB, "QuizSetting")
NumberOfQuestionsToBeInserted = dsQuizDB.Tables("QuizSetting").Rows.Item(0).Field(Of Integer)("NoOfQuestions")
Catch err As Exception
showErrorMessages.Text = err.Message
Return -1
Finally
con.Close()
End Try
Return NumberOfQuestionsToBeInserted
End Function
'function for Returning the Total number of questions to be inserted
Private Function GetNumberOfQuestionsInserted() As Integer
Dim SQLStatement As String
Dim NumberOfQuestionsInserted As Integer
SQLStatement = "SELECT Count(*) as NoOfQuestionsAlreadyInserted from Question"
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(SQLStatement, con)
Dim adapter As New SqlDataAdapter(cmd)
Dim dsQuizDB As New DataSet()
' Try to open database and read information.
Try
con.Open()
adapter.Fill(dsQuizDB, "QuizSetting")
NumberOfQuestionsInserted = dsQuizDB.Tables("QuizSetting").Rows.Item(0).Field(Of Integer)("NoOfQuestionsAlreadyInserted")
Catch err As Exception
showErrorMessages.Text = err.Message
Return -1
Finally
con.Close()
End Try
Return NumberOfQuestionsInserted
End Function
End Class