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.
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.
- 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:
- 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.
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."
4 comments
Click here for commentssir thanks for the coding,but i am getting error
Replyon this coding.
error is tag not closed
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.
ReplyrblQualification.DataSource = dt;
ReplyrblQualification.DataTextField = "Qualification";
rblQualification.DataValueField = "Qualification_ID";
rblQualification.DataBind();
what is rbl
rbl is short name for RadioButtonList ..
ReplyIf 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..