Start New Discussion within our Software Development Community

Is it possible that when writing this:

Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter


        da = New OleDbDataAdapter("SELECT * FROM [Football Questions]", con)
        da.Fill(dt)

        lblQuestion.Text = dt.Rows(0).Item(1)
        lblAnswer1.Text = dt.Rows(0).Item(2)
        lblAnswer2.Text = dt.Rows(0).Item(3)
        lblAnswer3.Text = dt.Rows(0).Item(4)
        lblAnswer4.Text = dt.Rows(0).Item(5)
        lblCorrectAnswer.Tag = dt.Rows(0).Item(6)
        radAnswer1.Tag = dt.Rows(0).Item(2)
        radAnswer2.Tag = dt.Rows(0).Item(3)
        radAnswer3.Tag = dt.Rows(0).Item(4)
        radAnswer4.Tag = dt.Rows(0).Item(5)

        con.Close()

Can I replace the "0" from "dt.Rows(0)" with a variable that I can increment to cycle through each question in the database?

I currently have this to run when the confirm button is clicked and it works for the first question but further than that I'm stuck as how to show the third question

 If radAnswer1.Checked Then
            If lblAnswer1.Text = lblCorrectAnswer.Tag Then
                Score += 1
                Dim ds As New DataSet
                Dim dt As New DataTable
                ds.Tables.Add(dt)
                Dim da As New OleDbDataAdapter

                da = New OleDbDataAdapter("SELECT * FROM [Football Questions]", con)
                da.Fill(dt)

                lblQuestion.Text = dt.Rows(1).Item(1)
                lblAnswer1.Text = dt.Rows(1).Item(2)
                lblAnswer2.Text = dt.Rows(1).Item(3)
                lblAnswer3.Text = dt.Rows(1).Item(4)
                lblAnswer4.Text = dt.Rows(1).Item(5)
                lblCorrectAnswer.Tag = dt.Rows(1).Item(6)
                radAnswer1.Tag = dt.Rows(1).Item(2)
                radAnswer2.Tag = dt.Rows(1).Item(3)
                radAnswer3.Tag = dt.Rows(1).Item(4)
                radAnswer4.Tag = dt.Rows(1).Item(5)

                con.Close()

            End If

One way is to store the number of the current question in the Tag property of the button. Then you can put it, cast to an integer, in a variable and use that instead of a literal number. Then increment it and replace the Tag value with that.

button1.Tag = 1
Sub button1_Click(sender As Object, e As EventArgs)
    Dim questionNumber As Integer = DirectCast(button1.Tag, Integer)

    'Do stuff'

    button1.Tag = questionNumber + 1
End Sub

On a side note, unless you're changing the data table, you would be better off to make the data table class level in scope and read the data once.

This article has been dead for over six months. Start a new discussion instead.