Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
I have a radio button list, and i want the correct radio button to be checked when i retrieve its value from the database and also highlight it. This is the code i use to retrieve the radio button values but it doesn't check them, but i want it to retrieve all the values into the radio button list and also check the one radio button i preferred value.

This is how i do it:

VB
For i = 0 To trnum
               cmd.CommandText = "select * from test where Serial=" & a(i)
               adp = New SqlDataAdapter(cmd.CommandText, cnn)
               adp.Fill(ds, "test")
           Next




           cnn.Close() 'connection closed


           dt = New DataTable("Answered")
           dt.Columns.Add("Serial", GetType(Integer))
           dt.Columns.Add("question", GetType(String))
           dt.Columns.Add("choice1", GetType(String))
           dt.Columns.Add("choice2", GetType(String))
           dt.Columns.Add("choice3", GetType(String))
           dt.Columns.Add("choice4", GetType(String))
           dt.Columns.Add("correct", GetType(String))
           dt.Columns.Add("selected", GetType(Integer))
           dt.Columns.Add("explain", GetType(String))


           Dim r As DataRow

           For Each r In ds.Tables("test").Rows
               dr = dt.NewRow
               dr("Serial") = dt.Rows.Count + 1
               dr("question") = r.Item("question")
               dr("choice1") = r.Item("choice1")
               dr("choice2") = r.Item("choice2")
               dr("choice3") = r.Item("choice3")
               dr("choice4") = r.Item("choice4")
               dr("correct") = r.Item("correct")
               dr("explain") = r.Item("explain")
               dr("selected") = -1
               dt.Rows.Add(dr)

           Next

           Session("Answered") = dt

           Call show()



VB
Sub show()
       dt = Session("Answered")
       Dim v As View = Me.View1


       Dim l As Label
       l = CType(v.FindControl("Label1"), Label)
       l.Text = dt.Rows(ctr).Item("Serial") & "."
       l = CType(v.FindControl("Label2"), Label)
       l.Text = dt.Rows(ctr).Item("question")



       Dim r As RadioButtonList
       r = CType(v.FindControl("RadioButtonList1"), RadioButtonList)
       r.Items.Clear()
       r.Items.Add(dt.Rows(ctr).Item("choice1"))
       r.Items.Add(dt.Rows(ctr).Item("choice2"))
       r.Items.Add(dt.Rows(ctr).Item("choice3"))
       r.Items.Add(dt.Rows(ctr).Item("choice4"))
       r.SelectedIndex = dt.Rows(ctr).Item("selected"

)



Please help me with a correct code or a link to help, thanks
Posted
Updated 29-Mar-13 0:24am
v5
Comments
pradiprenushe 29-Mar-13 7:09am    
Check each choice with correct column after adding item. If it is equal set that item selected.
OsoJames 29-Mar-13 7:14am    
ok...can you please help me with those lines of codes. How do i code that?
pradiprenushe 29-Mar-13 7:34am    
Try my answer below

When you return the values from the database Either you will be saving Text or value.

use this if you are saving value to database

VB
RadioButtonList1.Items.FindByValue("your string value from database").Selected = True


if you are saving text use this:

VB
RadioButtonList1.Items.FindByText("your string value from database").Selected = True


Try this.it works
 
Share this answer
 
Comments
OsoJames 29-Mar-13 6:14am    
Thank you Nandakishorerao, i tried your example but i gives me this error:
"Object reference not set to an instance of an object."
In my database i store the correct answer as an 'int' but not as text, so it probably check against the number of items in the radio button list, anyway on how to handle that?
Thank you again
Nandakishore G N 29-Mar-13 6:21am    
ok..how are you loading data to radiobuttonlist..because it makes easy for me and the other developers to help to sort.out..paste it in your question using improve question.
OsoJames 29-Mar-13 6:26am    
0k,,I've improved the question. Hope it helps
OsoJames 29-Mar-13 9:18am    
Thank you, that's why i always ask questions when i'm stuck during coding, it helps me to always solve the problem. Solved it finally. Thanks again
VB
r.Items.Add(dt.Rows(ctr).Item("choice1"))
       r.Items.Add(dt.Rows(ctr).Item("choice2"))
       r.Items.Add(dt.Rows(ctr).Item("choice3"))
       r.Items.Add(dt.Rows(ctr).Item("choice4"))


Add one of these line below your code
VB
r.Items.FindByValue(dt.Rows(ctr).Item("correct").ToString()).Selected = True
or
r.SelectedValue = dt.Rows(ctr).Item("correct").ToString()


if you want to use selected column as index then

VB
r.SelectedIndex = Convert.ToInt32(dt.Rows(ctr).Item("selected"))
 
Share this answer
 
v3
Comments
OsoJames 29-Mar-13 8:18am    
I already have the code below, only that i don't convert it but it works just fine
r.SelectedIndex = dt.Rows(ctr).Item("selected")

And i tried both the codes above to see if it could automatically check the right radio button by retrieving its correct value from the database but it's still giving me the same error:
"object reference not set to an instance of the object"
pradiprenushe 29-Mar-13 8:22am    
At which line you are getting this exception?
pradiprenushe 29-Mar-13 8:24am    
Where this ctr variable come from? does it having value? What are values in correct column?
OsoJames 29-Mar-13 8:38am    
The exception is at the line i put the code, and the correct column holds integer values.
OsoJames 29-Mar-13 9:17am    
Thank you, that's why i always ask questions when i'm stuck during coding, it helps me to always solve the problem. Solved it finally. Thanks again
This was what i needed to add to let it select or check the right answer automatically:

Me.RadioButtonList1.SelectedIndex = dt.Rows(ctr).Item("correct") - 1


Thank you great programmers on Code Project for being good teachers
 
Share this answer
 

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