multiple choice in vbnet help

Joined
Apr 6, 2005
Messages
6
Programming Experience
Beginner
i have created a database with two tables,(tblQuestions and tblAnswers),the aim is to pull one questions at a time from the tblQuestions and using radion buttons pull 5 answers for each question randomly from the table.I am using textboxes for the questions with five radiobuttons below it with thier texts being the answers.Then a submit button will carry on to the next question but i cant get the question table to read the next row it just reads the first row again.


the problem is the that the following code only takes the first question from the table and not the next row and for the answers goes stright to the last row rather than the correct answer.

Any ideas or advise on how to go about this correctly welcome.

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



		Try
		 cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\project.mdb;")
			

			cn.Open()
			cmd = New OleDbCommand("select * from tblQuestions", cn)
			dr = cmd.ExecuteReader

			While dr.Read()
				
				Label3.Text = dr(0)
				TextBox2.Text = dr(1)
				TextBox3.Text = dr(2)

				TextBox7.Text = dr(3)
				TextBox8.Text = dr(4)
				TextBox9.Text = dr(5)

				
			End While
		Catch
		End Try
		dr.Close()
		cn.Close()

	End Sub

------------------------  ------------------------
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Try
		 cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\project.mdb;")
			
			cn.Open()
			cmd = New OleDbCommand("select Answer from tblAnswers", cn)
			dr = cmd.ExecuteReader
			While dr.Read()
				TextBox4.Text = dr(0)
				TextBox5.Text = dr(1)
				RadioButton1.Text = dr(0)

				
			End While
		Catch
		End Try
		dr.Close()
		cn.Close()
	End Sub
dr(0) etc only takes me to a row how can i get it to go down the list as well.
My apologies if i have asked to much or posted incorrectly but am desperate now.
 
Last edited:

dbaldwin

New member
Joined
Apr 25, 2005
Messages
2
Programming Experience
5-10
I think your Button2_Click event requeries the tblAnswers. To minimize your code changes you would need to define cn and dr (maybe call it mcn and mdr) at the form level, and run in the form .load
mcn.Open()
cmd = New OleDbCommand("select Answer from tblAnswers", mcn)
mdr = cmd.ExecuteReader
then
do your
While mdr.Read() ... loop in Button2_Click

when your done don't forget to close your datareader and connection.

-----

Personally I would define the questions, answers, and correct answer in the same table. Then load the table into a System.Data.DataTable object. Then cycle through each DataTable.Row as needed.
 
Joined
Apr 6, 2005
Messages
6
Programming Experience
Beginner
thanks for your help,i used select statemets and went the long way round and had to remove bits and pieces but the work was submitted yesterday.Now i can work on it at my lesiure untill i understand everything better.
 
Top Bottom