Introduction:
Now in this article we will bind RadioButtonList with Sql Server Database table using asp.net.
Now in this article we will bind RadioButtonList with Sql Server Database table using asp.net.
Sql database Table:
Create a
connectionstring in the web.config:
<connectionStrings>
<add name="Emp_Con" connectionString="Data Source=localhost;Initial Catalog=test;Integrated
Security=True"/>
</connectionStrings>
Now in design page(.aspx) place a RadioButtonList control as :
Now in design page(.aspx) place a RadioButtonList control as :
<fieldset style="width:360px;height:160px;">
<legend>Fill
Radiobuttolist</legend>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="3"
RepeatDirection="Horizontal">
</asp:RadioButtonList>
</fieldset>
C#.Net Code to Bind/Fill
RadioButtonList from Sql server table data in asp.ne
In the code behind file (.aspx.cs) write the code as:
Include following
namespaces:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindRadioButtonList ();
}
}
private void
BindRadioButtonList()
{
SqlConnection
con = new SqlConnection(ConfigurationManager.ConnectionStrings["EmpCon"].ConnectionString);
SqlCommand
cmd = new SqlCommand("Select * from Dept_Table", con);
SqlDataAdapter
adp = new SqlDataAdapter(cmd);
DataTable
dt = new DataTable();
adp.Fill(dt);
RadioButtonList1.DataSource = dt;
RadioButtonList1.DataTextField = "Dept_Name";
RadioButtonList1.DataValueField = "Dept_Id_Pk";
RadioButtonList1.DataBind();
}
VB.Net Code to Bind/Fill RadioButtonList from
Sql server table data in asp.ne
In the code behind file (.aspx.vb) write the code as:
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
BindRadioButtonList ()
End If
End Sub
Private Sub BindRadioButtonList ()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("EmpCon ").ConnectionString)
Dim cmd As New SqlCommand("Select * from Dept_Table", con)
Dim adp As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
adp.Fill(dt)
RadioButtonList1.DataSource
= dt
RadioButtonList1.DataTextField
= "Dept_Name"
RadioButtonList1.DataValueField =
"Dept_Id_Pk"
RadioButtonList1.DataBind()
End Sub
0 comments:
Post a Comment