How to Bind/Fill RadioButtonList from Sql server table in asp.net(C#, VB)

Introduction: In previous examples I explained How to fill DropDownList from Sql server database in asp.net and How to Fill CheckBoxList from Sql Server table in asp.net(C#,VB).  and How to Bind/Load/Fill ListBox with Sql Server Database in asp.net and Fill DropDownList from Sql server database in asp.net and How to Create,Read,Sort Xml file and Bind to RadioButtonList
Now in this article i will explain how to Fill/Bind/Load RadioButtonList from Sql Server Database table using asp.net

Implementation: Let's understand by an example.

Fill RadioButtonList example in asp.net
  • Create a Database in sql and name it "MyDataBase" and also create a table in that database with the columns and data type as shown in Fig below and name it QUALIFICATION_TABLE:

Bind RadioButtonList example in asp.net
  • Create a connectionstring in the web.config file under configuration tag as:
<connectionStrings>
    <add name="ConStr" connectionString="Data Source=LocalServer;Initial Catalog=MyDataBase;Integrated Security=True"/>
  </connectionStrings> 

Note: Replace the Data Source and Initial Catalog(database name) as per your application.
  • Now in design page(.aspx) place a RadioButtonList control as :
<fieldset style="width:250px;">
            <legend>Fill RadioButtonList Example in asp.net</legend>
             <asp:RadioButtonList ID="rblQualification" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"></asp:RadioButtonList>   
        </fieldset>

C#.Net Code to Bind/Fill RadioButtonList from Sql server table data in asp.net
  • In the code behind file (.aspx.cs) write the code as:
First include following namespaces:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillQualifications();
        }
    }
    private void FillQualifications()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
        SqlCommand cmd = new SqlCommand("Select * from Qualification_Table", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        rblQualification.DataSource = dt;
        rblQualification.DataTextField = "Qualification";
        rblQualification.DataValueField = "Qualification_ID";
        rblQualification.DataBind();
    }

VB.Net Code to Bind/Fill RadioButtonList from Sql server table data in asp.net
  • In the code behind file (.aspx.vb) write the code as:
First include following namespaces:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            FillQualifications()
        End If
    End Sub

    Private Sub FillQualifications()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConStr").ConnectionString)
        Dim cmd As New SqlCommand("Select * from Qualification_Table", con)
        Dim adp As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        adp.Fill(dt)
        rblQualification.DataSource = dt
        rblQualification.DataTextField = "Qualification"
        rblQualification.DataValueField = "Qualification_ID"
        rblQualification.DataBind()
    End Sub

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

4 comments

Click here for comments
Unknown
admin
August 18, 2013 ×

sir thanks for the coding,but i am getting error
on this coding.




error is tag not closed

Reply
avatar
August 18, 2013 ×

hello shanawaj thanks for your feedback..i suggest you to thoroughly recheck the source code and the code in code behind because this code is tested and working.If still your error not solved then let me know the exact line where your are getting error..i will help you resolve it.

Reply
avatar
Islam
admin
May 27, 2014 ×

rblQualification.DataSource = dt;
rblQualification.DataTextField = "Qualification";
rblQualification.DataValueField = "Qualification_ID";
rblQualification.DataBind();
what is rbl

Reply
avatar
May 27, 2014 ×

rbl is short name for RadioButtonList ..

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..