Question

I have create multiple choice system where the user need to choose answer from 4 radiobutton before navigate to next question by clicking next button.

So, the I have problem in storing the selected radiobutton into database. At first I create a table where I create columns to store each answer but it failed. Now, I'm stuck. Please give any suggestion where i can store the answer.

Here I provide the code to load the question

p/s: I know i should ask this on different thread but I also stuck on how to randomize the radiobutton.

Public Property Counter() As Integer
    Get
        Return IIf(ViewState("counter") Is Nothing, 1, CInt(ViewState("counter")))
    End Get
    Set(ByVal value As Integer
        ViewState("counter") = value
    End Set
End Property

Protected Sub Next_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Counter += 1
        question()
        clean()

End Sub
Sub question()
conn.Open()

    Dim cmd As New SqlCommand("Select * From ques Where Id=@Id", conn)
    cmd.Parameters.AddWithValue("@Id", Counter)
    Dim dr1 As SqlDataReader
    dr1 = cmd.ExecuteReader

        If dr1.Read() Then
            Me.lblquestion.Text = dr1("question")
            Me.RadioButton1.Text = dr1("right")
            Me.RadioButton2.Text = dr1("wrong")
            Me.RadioButton3.Text = dr1("wrong2")
            Me.RadioButton4.Text = dr1("wrong3")


    Else
        conn.Close()
        Counter += 1
        question()
    End If
    conn.Close()


End Sub
Sub clean()
    RadioButton1.Checked = False
    RadioButton2.Checked = False
    RadioButton3.Checked = False
    RadioButton4.Checked = False
    Next.Enabled = False
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    question()
End Sub

Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    Next.Enabled = True
End Sub

Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
    Next.Enabled = True
End Sub

Protected Sub RadioButton3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
    Next.Enabled = True
End Sub

Protected Sub RadioButton4_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
    Next.Enabled = True
End Sub
End Class
Was it helpful?

Solution

I would have a database structure like this:

TblQuizes

Quiz_Id int 
Quiz_Name nvarchar(200) -- or any other number that meets your demands

TblQuestions

Question_Id int 
Question_Quiz_Id int --(FK To TblQuizes) 
Question_Text nvarchar(200) -- or any other number that meets your demands

TblAnswers

Answer_Id int 
Answer_Question_Id int --(FK To TblQuestions) 
Answer_Text nvarchar(200) -- or any other number that meets your demands 
Answer_IsCorrect bit

TblResults

Result_Id int 
Result_Quiz_Id int -- (FK to TblQuizes) (not even needed, just to convenience)
-- Add user data in this table if you need it

TblResultDetails

ResultDetails_Id int
ResultDetails_Result_Id int --(FK to TblResults) 
ResultDetails_Answer_Id int (FK to TblAnswers)

Now what I would do is create a new record in TblResults for every user that answers the quiz, and store the users answers in TblResultDetails. On this basic structure you can create views to give you the entire data you need to show the quiz to the user, or to show him how well he answered your questions, and so on. you can also see very easily if a user did not complete the quiz, just by comparing the number of records in TblQuetions and TblResultDetails for a specific quiz id.

Also, if you ever want to have questions with only 2 answer, or 6 answers, you don't have to change anything in the database structure or in your code to have that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top