How To bind RadioButtonList Control From data base

Radio List Control bind from Sql database:

In Asp.net programming tutorial today I want to see “Binding of RadiobuttonList from Sql data base”. as we know that Button list control is used to create a group of radio buttons.In this the list elements are used to define items in Button groups. And we also know buttonlist control support data from the data base and display this data as a buttonlist items.



Use Sql data Base And Display data in RadioButton control List:

Asp.net web programming by C# in which the developer face problem to binding data in Radiobutton. The most important Question is this. When we visit many web sites for solving this Question

Data binding Example of Button control:


 I remember that when we starting Asp.net web development then we face this type of problems much time.
Here we just try to display data into radio button from database.

Asp.net code for .aspx page for declare a buttonlist:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:radiobuttonlist ID="rblCountryList" runat="server"></asp:radiobuttonlist>
    </div>
    <div>
     <asp:Button ID="btn" runat="server" Text="ok" OnClick="btnSubmit_Click" /><br />
     <asp:Label ID="lblMsg" runat="server"></asp:Label> 
    </div>
    </form>
</body>
</html>

Asp.net code with C# for .aspx.cs page for bind data buttonlist:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Demo;User ID=sa;Password=sa123");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindList();// call function here........................
        }
    }

    void BindList()//...........define function here......................
    {
        DataSet ds = new DataSet();//create data base instanse...................
        string cmdstr = "select id,country from Countrylist";//sql query for fecthing data..............
        SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
        adp.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            rblCountryList.DataSource = ds;
            rblCountryList.DataTextField = "country";
            rblCountryList.DataValueField = "id";
            rblCountryList.DataBind();
        }
        else
        {
            lblMsg.Text = "There are no dat in data base ..................";
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMsg.Text = rblCountryList.SelectedItem.ToString();

    }
}



 Other Radio Button Related Post:




Comments

Post a Comment

Popular posts from this blog