Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
The following is my code, I am doing project on online examination.when i click on next button the questions are repeated

VB
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    If Not IsPostBack Then
        Try
            Dim daQuestions As New SqlDataAdapter("SELECT TOP 20 * FROM tblQuestions ORDER BY NEWID()", con)
            Dim dsQuestions As New Data.DataSet
            Dim dtQuestions As New Data.DataTable

            If Not con.State = Data.ConnectionState.Open Then con.Open()
            daQuestions.Fill(dsQuestions)
            dtQuestions = dsQuestions.Tables(0)

            If Not dtQuestions.Rows.Count <= 0 Then
                'For Question 1
                If Not IsDBNull(dtQuestions.Rows(0).Item("Question")) Then
                    lblQuestion1.Text = dtQuestions.Rows(0).Item("Question")
                End If
                If Not IsDBNull(dtQuestions.Rows(0).Item("Option1")) Then
                    rblQuestion1.Items(0).Text = dtQuestions.Rows(0).Item("Option1")
                End If
                If Not IsDBNull(dtQuestions.Rows(0).Item("Option2")) Then
                    rblQuestion1.Items(1).Text = dtQuestions.Rows(0).Item("Option2")
                End If
                If Not IsDBNull(dtQuestions.Rows(0).Item("Option3")) Then
                    rblQuestion1.Items(2).Text = dtQuestions.Rows(0).Item("Option3")
                End If
                If Not IsDBNull(dtQuestions.Rows(0).Item("Option4")) Then
                    rblQuestion1.Items(3).Text = dtQuestions.Rows(0).Item("Option4")
                End If
            End If
        Catch ex As Exception

        End Try




        'con.ConnectionString = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
        cmd = New SqlCommand("insert into tblQuestions (Question, Option1, Option2, Option3, Option4,) values (@question, @option1, @option2, @option3, @option4)", con)
        cmd.Parameters.AddWithValue("@question", lblQuestion1.Text)
        cmd.Parameters.AddWithValue("@option1", rblQuestion1.Text)
        cmd.Parameters.AddWithValue("@option2", rblQuestion1.Text)
        cmd.Parameters.AddWithValue("@option3", rblQuestion1.Text)
        cmd.Parameters.AddWithValue("@option4", rblQuestion1.Text)
        'cmdInsertQuestion.Parameters.AddWithValue("@answer", txtAnswer.Text)
        Try
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            MsgBox(" ")
            'txtAnswer.Text = ""
            rblQuestion1.Text = ""
            rblQuestion1.Text = ""
            rblQuestion1.Text = ""
            rblQuestion1.Text = ""
        Catch ex As Exception
            'ex.Message
        End Try
    End If
End Sub



Protected Sub rblQuestion1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rblQuestion1.SelectedIndexChanged

End Sub

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


    If IsPostBack Then
        Try
            Dim daQuestions As New SqlDataAdapter("SELECT TOP 20 * FROM tblQuestions ORDER BY NEWID()", con)
            Dim dsQuestions As New Data.DataSet
            Dim dtQuestions As New Data.DataTable
            rblQuestion1.ClearSelection()
            If Not con.State = Data.ConnectionState.Open Then con.Open()
            daQuestions.Fill(dsQuestions)
            dtQuestions = dsQuestions.Tables(0)

            If Not dtQuestions.Rows.Count <= 0 Then

                If Not IsDBNull(dtQuestions.Rows(0).Item("Question")) Then
                    lblQuestion1.Text = dtQuestions.Rows(0).Item("Question")
                End If
                If Not IsDBNull(dtQuestions.Rows(0).Item("Option1")) Then
                    rblQuestion1.Items(0).Text = dtQuestions.Rows(0).Item("Option1")
                End If
                If Not IsDBNull(dtQuestions.Rows(0).Item("Option2")) Then
                    rblQuestion1.Items(1).Text = dtQuestions.Rows(0).Item("Option2")
                End If
                If Not IsDBNull(dtQuestions.Rows(0).Item("Option3")) Then
                    rblQuestion1.Items(2).Text = dtQuestions.Rows(0).Item("Option3")
                End If
                If Not IsDBNull(dtQuestions.Rows(0).Item("Option4")) Then
                    rblQuestion1.Items(3).Text = dtQuestions.Rows(0).Item("Option4")
                End If
            End If



        Catch ex As Exception

        End Try


    End If
End Sub



End Class




plz anyone can help me plzzzz
Posted
Updated 25-May-15 22:19pm
v5
Comments
F-ES Sitecore 26-May-15 4:36am    
You are getting all the questions in a random order each time, it has no idea what questions have been previously asked so what questions it should not ask again. Create a List<int> ("List of int" in vb I think) and each time a question is asked, add the question ID to that list and store the list in the session. Now when you show a question, rather than using Row(0), you loop through the rows until you find a question ID that is not in your list of previously asked questions.

To pre-empt your next question, no I won't "give you the code", this is obviously your homework so you are being tested on your academic ability and getting other people to do your work is not academic ability. See your tutor if you have further questions or use google.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900