I have created an exam using the control wizard. Most answers are text answers I created the SQL db. Now when I try to read the data it always gives me cast error.
I am new at this but need to figure a way to get it done. The only solution I had was the one attached but there has to be a way to use an array or create a new web ui control from a string.
When I try to do this: QLabel = {"QLabel" & (i) & ".text") and use I get an error tried using DirectCast on QLable and I get a cast error any suggestions?
One side note all tests would be text only or radiobuttonlist only not mixed like this one.
The code is below.
Expand|Select|Wrap|Line Numbers
- Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick
- 'Get Membership GUID
- 'Label2.Text = "Geeting User"
- Dim instance As MembershipUser = Membership.GetUser()
- Dim value As Object
- If instance Is Nothing Then
- Label2.Text = "NO USER IS LOGGED IN"
- Exit Sub
- End If
- value = instance.ProviderUserKey.ToString
- Dim NameId = value.ToString
- 'SQL Input
- Dim QGroup
- Dim QAnswer As Object
- Dim QLabel
- QGroup = Profile.Opportunity
- Dim i As Integer
- i = 0
- Do Until i > 26
- i = i + 1
- QAnswer = ("QAnswer" & (i) & ".Text")
- QLabel = ("QLabel" & (i) & ".Text")
- If i = 1 Then
- QLabel = QLabel1.Text
- QAnswer = QAnswer1.Text
- ElseIf i = 2 Then
- QLabel = QLabel2.Text
- QAnswer = QAnswer2.Text
- ElseIf i = 3 Then
- QLabel = QLabel3.Text
- QAnswer = QAnswer3.Text
- ElseIf i = 4 Then
- QLabel = QLabel4.Text
- QAnswer = QAnswer4.Text
- ElseIf i = 5 Then
- QLabel = QLabel5.Text
- QAnswer = QAnswer5.Text
- ElseIf i = 6 Then
- QLabel = QLabel6.Text
- QAnswer = QAnswer6.Text
- ElseIf i = 7 Then
- QLabel = QLabel7.Text
- QAnswer = QAnswer7.Text
- ElseIf i = 8 Then
- QLabel = QLabel8.Text
- QAnswer = QAnswer8.Text
- ElseIf i = 9 Then
- QLabel = QLabel9.Text
- QAnswer = QAnswer9.Text
- ElseIf i = 10 Then
- QLabel = QLabel10.Text
- QAnswer = QAnswer10.Text
- ElseIf i = 11 Then
- QLabel = QLabel11.Text
- QAnswer = QAnswer11.Text
- ElseIf i = 12 Then
- QLabel = QLabel12.Text
- QAnswer = QAnswer12.Text
- ElseIf i = 13 Then
- QLabel = QLabel13.Text
- QAnswer = RadioButtonList3.SelectedValue
- ElseIf i = 14 Then
- QLabel = QLabel14.Text
- QAnswer = QAnswer14.Text
- ElseIf i = 15 Then
- QLabel = QLabel15.Text
- QAnswer = QAnswer15.Text
- ElseIf i = 16 Then
- QLabel = QLabel16.Text
- QAnswer = QAnswer16.Text
- ElseIf i = 17 Then
- QLabel = QLabel17.Text
- QAnswer = QAnswer17.Text
- ElseIf i = 18 Then
- QLabel = QLabel18.Text
- QAnswer = QAnswer18.Text
- ElseIf i = 19 Then
- QLabel = QLabel19.Text
- QAnswer = QAnswer19.Text
- ElseIf i = 20 Then
- QLabel = "Completed"
- QAnswer = "EURLA"
- ElseIf i = 21 Then
- QLabel = QLabel21.Text
- QAnswer = RadioButtonList1.SelectedValue
- ElseIf i = 22 Then
- QLabel = QLabel22.Text
- QAnswer = DropDownList1.SelectedValue
- ElseIf i = 23 Then
- QLabel = QLabel23.Text
- QAnswer = DropDownList2.SelectedValue
- ElseIf i = 24 Then
- QLabel = QLabel24.Text
- QAnswer = DropDownList3.SelectedValue
- ElseIf i = 25 Then
- QLabel = QLabel25.Text
- QAnswer = DropDownList4.SelectedValue
- ElseIf i = 26 Then
- QLabel = QLabel26.Text
- QAnswer = DropDownList5.SelectedValue
- ElseIf i = 27 Then
- QLabel = QLabel27.Text
- QAnswer = RadioButtonList2.SelectedValue
- End If
- 'If QLabel.GetType().ToString() = "System.Web.UI.WebControls.Label" Then
- 'Dim txtLabel As Label = DirectCast(QLabel, Label)
- 'txtLabel.Text = Trim(txtLabel.Text)
- 'End If
- Dim connStr As String = ConfigurationManager.ConnectionStrings("MyProfileConnectionString").ConnectionString
- Dim myConnection As New SqlConnection(connStr)
- Const SQL As String = "INSERT INTO[Questions]([UserID], [QuestionGroup], [QuestLabel], [QuestAns]) Values(@UserID, @QGroup, @QLabel, @QAnswer)"
- Dim myCommand As New SqlCommand(SQL, myConnection)
- myCommand.Parameters.AddWithValue("@UserID", NameId)
- myCommand.Parameters.AddWithValue("@QGroup", QGroup)
- myCommand.Parameters.AddWithValue("@QLabel", QLabel)
- myCommand.Parameters.AddWithValue("@QAnswer", QAnswer)
- 'For Testing Only
- 'Label2.Text = NameId
- Try
- myConnection.Open()
- myCommand.ExecuteNonQuery()
- myConnection.Close()
- Catch ex As Exception
- 'For Testing Only
- Label1.Visible = True
- Label1.Text = ex.Message.ToString
- End Try
- Loop