In this article I will explain with an example, how to bind / fill / populate data to RadioButtonList control from SQL Server database records in ASP.Net using C# and VB.Net.
The data used to bind / fill / populate the RadioButtonList control will be fetched from SQL Server database table using DataReader.
 
 
Database
This article makes use of a table named Fruits whose schema is defined as follows.
Bind / Fill / Populate RadioButtonList control from database in ASP.Net using C# and VB.Net
 
The Fruits table has the following records.
Bind / Fill / Populate RadioButtonList control from database in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

HTML Markup
The HTML Markup consists of an ASP.Net RadioButtonList which will be populated from database and a Button control.
<asp:RadioButtonList ID="rblFruits" runat="server">
</asp:RadioButtonList>
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Submit" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Populating RadioButtonList control from database in ASP.Net
Inside the Page Load event of the page, the RadioButtonList is populated with the records of the Fruits Table.
The FruitId and the FruitName column values are fetched from the database using SqlDataReader and are assigned to the DataTextField and DataValueField properties of the RadioButtonList control.
DataTextField – The values of the column set as DataTextField are visible to the user.
DataValueField – The values of the column set as DataValueField are not visible to the user. Generally ID or Primary Key columns are set as values in order to uniquely identify a RadioButtonList Item.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = "SELECT FruitName, FruitId FROM Fruits";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                rblFruits.DataSource = cmd.ExecuteReader();
                rblFruits.DataTextField = "FruitName";
                rblFruits.DataValueField = "FruitId";
                rblFruits.DataBind();
                con.Close();
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("Constring").ConnectionString
        Using con As New SqlConnection(constr)
            Dim query As String = "SELECT FruitName, FruitId FROM Fruits"
            Using cmd As New SqlCommand(query)
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                con.Open()
                rblFruits.DataSource = cmd.ExecuteReader()
                rblFruits.DataTextField = "FruitName"
                rblFruits.DataValueField = "FruitId"
                rblFruits.DataBind()
                con.Close()
            End Using
        End Using
    End If
End Sub
 
 
Getting the Selected Text and Value of the ASP.Net RadioButtonList
When the Button is clicked, the Value and the Text part of the Selected Item of the ASP.Net RadioButtonList is fetched and displayed using JavaScript Alert message box.
C#
protected void Submit(object sender, EventArgs e)
{
    string message = "Value: " + rblFruits.SelectedItem.Value;
    message += " Text: " + rblFruits.SelectedItem.Text;
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
 
VB.Net
Protected Sub Submit(sender As Object, e As EventArgs)
    Dim message As String = "Value: " & rblFruits.SelectedItem.Value
    message &= " Text: " & rblFruits.SelectedItem.Text
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
 
 
Screenshot
Bind / Fill / Populate RadioButtonList control from database in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads