Hi All,

I am hoping someone can help me with this and/or point me in the right direction.
I am developing a quiz application and have run into a problem when the question being pulled from the database is a true/false question it produces an error, straight multiple choice questions are working fine.

Here are the variable declerations I am using:

'Declare our varaiables
    Dim inc As Integer
    Public MaxRows As Integer
    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String
    Dim cmd As OleDbCommand
    Dim NextTime As Date = Now
    Dim score As Integer
    Dim tbTable As String
    Dim qnum As Integer = "1"     'Sets up variable to hold what question number the user is currently on

Here is the section of code I belive is the problem.

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click

        'Check to see if current answer is correct
        'If so increment the score counter accordingly

        If RadioButton1.Checked = True Then
            If RadioButton1.Text = ds.Tables("Test").Rows(inc).Item(6) Then
                score = score + 1
                Label3.Text = score / MaxRows * 100         'Score divided by questions *100
            Else
                MsgBox("The correct answer is: " + ds.Tables("Test").Rows(inc).Item(6), MsgBoxStyle.Exclamation)
            End If

        ElseIf RadioButton2.Checked = True Then
            If RadioButton2.Text = ds.Tables("Test").Rows(inc).Item(6) Then
                score = score + 1
                Label3.Text = score / MaxRows * 100
            Else
                MsgBox("The correct answer is: " + ds.Tables("Test").Rows(inc).Item(6), MsgBoxStyle.Exclamation)

            End If

        ElseIf RadioButton3.Checked = True Then
            If RadioButton3.Text = ds.Tables("Test").Rows(inc).Item(6) Then
                score = score + 1
                Label3.Text = score / MaxRows * 100
            Else
                MsgBox("The correct answer is: " + ds.Tables("Test").Rows(inc).Item(6), MsgBoxStyle.Exclamation)

            End If

        ElseIf RadioButton4.Checked = True Then
            If RadioButton4.Text = ds.Tables("Test").Rows(inc).Item(6) Then
                score = score + 1
                Label3.Text = score / MaxRows * 100

            Else
                MsgBox("The correct answer is: " + ds.Tables("Test").Rows(inc).Item(6), MsgBoxStyle.Exclamation)

            End If

        End If

        'Move one record forward
        If inc <> MaxRows - 1 Then
            inc = inc + 1
            NavigateRecords()
        Else
            MsgBox("The test has been completed")
            Me.Hide()
            Results.Show()

        End If

    End Sub

and finally - I would aldo like to keep track of the questions that have been answered incorrectly and display them to the test taker at the end of the program, but not sure how to go about doing this. Any help, and guidance is greatly appreciated.

I am receiving an unhandled exception: "Conversion from type DBNull to type Sting is not valid".

Hi,

To handle Null fields.. use ".ToString", at the end..
However.. You can Simplify your code this way as well.....

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click    
    Dim CorrAns As String
    Dim UserAns As String
    '
    UserAns = ""
    If RadioButton1.Checked = True Then
        UserAns = RadioButton1.text
    ElseIf RadioButton2.Checked = True Then
        UserAns = RadioButton2.text
    ElseIf RadioButton3.Checked = True Then
        UserAns = RadioButton3.text
    ElseIf RadioButton4.Checked = True Then
        UserAns = RadioButton4.text
    End If
    CorrAns = ds.Tables("Test").Rows(inc).Item(6).ToString
    If UserAns = CorrAns Then
        score = score + 1
        Label3.text = score / MaxRows * 100
    Else
        MsgBox("The correct answer is: " + CorrAns, MsgBoxStyle.Exclamation)
    End If
    'Move one record forward
    If inc <> MaxRows - 1 Then
        inc = inc + 1
        NavigateRecords()
    Else
        MsgBox ("The test has been completed")
        Me.Hide()
        Results.Show()
    End If
End Sub

Regards
Veena

Thank you Veena - That seems to have solved the problem.
Good to know "To handle Null fields.. use ".ToString", at the end.." Thanks for providing that - I am sure it will save me some headaches in the future.

Any thoughts on how I can capture the incorrect answer and display them at the end of the quiz - so the quiz taker can study up on the incorrect answers?

Hi,

Yes... What you can do, is Add Class Module, and declare a Public Collection or Array in the Class..
Create an Instance of this Class in a Bas Module..
When the Answer is wrong, Add a member to the Collection..
In Results module, Loop thru all the members, and Display them in a ListBox/ grid..

Regards
Veena

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.