Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
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:
 
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()
 

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 28-Mar-13 17:28pm
Edited 29-Mar-13 1:24am
v5
Comments
pradiprenushe at 29-Mar-13 7:09am
   
Check each choice with correct column after adding item. If it is equal set that item selected.
OsoJames at 29-Mar-13 7:14am
   
ok...can you please help me with those lines of codes. How do i code that?
pradiprenushe at 29-Mar-13 7:34am
   
Try my answer below
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
 
RadioButtonList1.Items.FindByValue("your string value from database").Selected = True
 
if you are saving text use this:
 
RadioButtonList1.Items.FindByText("your string value from database").Selected = True
 
Try this.it works
  Permalink  
Comments
OsoJames at 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
Nandakishorerao at 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 at 29-Mar-13 6:26am
   
0k,,I've improved the question. Hope it helps
OsoJames at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
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
 
r.SelectedIndex = Convert.ToInt32(dt.Rows(ctr).Item("selected"))
  Permalink  
v3
Comments
OsoJames at 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 at 29-Mar-13 8:22am
   
At which line you are getting this exception?
pradiprenushe at 29-Mar-13 8:24am
   
Where this ctr variable come from? does it having value? What are values in correct column?
OsoJames at 29-Mar-13 8:38am
   
The exception is at the line i put the code, and the correct column holds integer values.
OsoJames at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 PIEBALDconsult 110
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100