Stupid Romeo Stupid Romeo - 2 years ago 91 Question

Get RadioButtonList value from database & selected

I am trying to populate My RadioButtonList values from database. That is working for me. But I am not getting how should I keep one predefined values in database to be selected by default in radiobuttonList. I have list of cities in my db table. One city I want to be selected when page loads. Following is my code

Private Sub MasterPage_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
End Sub

Private Sub PopulateCities()
Using conn As New MySqlConnection()
conn.ConnectionString = ConfigurationManager _
Using cmd As New MySqlCommand()
cmd.CommandText = "Select cityName from cities where status = 'active' order by cityName"
cmd.Connection = conn
Using sdr As MySqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Dim item As New ListItem()
item.Text = sdr("cityName").ToString()
item.Value = sdr("cityName").ToString()
item.Selected = Convert.ToBoolean(sdr("IsSelected"))
End While
End Using
End Using
End Using

Answer Source

If you database cities table contains IsSelected field then you need to change your query like this

Select cityName, IsSelected from cities where status = 'active' order by cityName

Elese you need to update your first list item

locationSelector.Items(0).Selected = True
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download