ASP.Net RadioButtonList Control with SQL DataBinding

Updated on 24 Jul 2012,
Published on 22 Mar 2010

The ASP.Net RadioButtonList control also supports SQL DataBinding feature that enables you to display the data retrieved from the SQL Database in the form of radio button type list items. You can either use the SqlDataSource control to bind the SQL data with radiobuttonlist control or C# code to bind the data programmatically. Using SqlDataSource control you can bind data items with radiobuttonlist control without writing any single line of C# code. You can display the data items by specifying the ID of the SqlDataSource control as a value of DataSourceID property of the RadioButtonList control. While using C# code to bind the SQL data dynamically you have to use the DataSource property of radiobuttonlist control to bind the data object. 

But specifying only DataSourceID or DataSource property of ASP.Net RadioButtonList control is not enough. You have to specify the values for 2 more properties to display the text content of radiobutton list items and set the value for each list item.

Following are the two properties that enable you to specify the text and value property of list items of RadioButtonList control:

DataTextField: It accepts the name of the Field of the data table retrieved using datasource control or data access C# code that you want to display as the text content of the list item.

DataValueField: It accepts the name of the field of the data table retrieved using datasource control or data access C# code that you want to set as the value attribute of each list item. This value part does not appear as text content.

Sample Code for ASP.Net RadioButtonList SQL DataBinding

HTML Code
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Value="item 1"></asp:ListItem>
    <asp:ListItem Value="item 2"></asp:ListItem>
    <asp:ListItem Value="item 3"></asp:ListItem>
    <asp:ListItem Value="item 4"></asp:ListItem>
    <asp:ListItem Value="item 5"></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
C# Code
public void BindRadioButtonListData()
{
    // connection string
    string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

    // Sql connection object initialized with connection string used to connect
    // it with Northwind SQL database
    using (SqlConnection mySqlConnection = new SqlConnection(connectionString))
    {
        try
        {
            // open the Sql connection
            mySqlConnection.Open();

            // Sql Command object initialized with SQL query to retrieve the categories
            SqlCommand mySqlCommand = new SqlCommand("Select CategoryID, CategoryName from Categories", mySqlConnection);

            // Sql Data Adapter object initialized by passing the Sql Command object
            SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySqlCommand);

 
            // DataSet object to store the retrieved SQL data items
            DataSet myDataSet = new DataSet();

            // fill the DataSet
            mySqlDataAdapter.Fill(myDataSet);

            // Set DataSet object as DataSource for the RadioButtonList
            RadioButtonList1.DataSource = myDataSet;

            // Specify the Field Name that you want to display as
            // text label for Radio Button list item
            RadioButtonList1.DataTextField = "CategoryName";

            // Specify the Field Name that you want to use as
            // value for each list item
            RadioButtonList1.DataValueField = "CategoryID";

            // Finalize the DataBinding
            RadioButtonList1.DataBind();

        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
        finally
        {
            // close the Sql Connection
            mySqlConnection.Close();
        }
    }
}

You can call the above function in the Page load event of the ASP.Net web page to bind the SQL data with RadioButtonList control.

Output:

Get the output for above discussed code from the following link:

RadioButtonList Control with SQL DataBinding

Continue to the next tutorial: Populating the ASP.Net RadioButtonList Control Items Dynamically to learn how to add the list items to the radio button list control programmatically.

4 Responses to "ASP.Net RadioButtonList Control with SQL DataBinding"
A.Chandramouli
Hi, I want to populate the data from the database with the help of radiobutton control in asp.net 3.5
Sumit
hi... i am making online exam project.In this i have took a radiobutton list and i have bound it with dataset table. Now i want to check radiobutto item values to the correct answer in dataset table how can i compare it with dataset table.
shilpa
good explination...
explained in simple words!!!
Thank you very much. very good explination
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email