how to bind drop down list form data base

Bind Drop down list form data base:

In the web development the Drop down list is most important tool, we Use many time and many places to use this for showing the list in hidden format.
And to fill date in to this list, when we need on page the select a particular item in this drop down list in to asp.net Programming.



Example:

I want to fill all class names in to drop down list which are store in the data base.


For this a have a table which has classes name.
asp.net by parijat
Sql data base table


we give some steps for this ......................
step 1:
know we create a web page as name Dropdown_list_Example.aspx
take a drop down list in to page
<asp:DropDownList ID="ddlclassId"
runat="server" Width="128px" AutoPostBack="True">                      
 </asp:DropDownList>

here we give the drop down list id is "ddlclassId" for understanding to drop down.
step 2:
then go to the coding page and make a function for selecting the value form data base.
 private void FillClass()
    {
        DataSet ds=new DataSet();
        ds=objc1.Select_Class();

/*
in the brown color coded part use for fetching data form table in to Dataset use some class file and Store processor for this .if you have other way then put here.
we select two values Class Name and Section Name in Class and Class Id ,Class id is primary key ..
*/

        ddlclassId.DataTextField = "Class";
//dataTextField hold those value which are show in list .
        ddlclassId.DataValueField = "ClassId";
//dataValueField use for get unicq value of selected item.
        ddlclassId.DataBind();
        ddlclassId.Items.Insert(0, new ListItem("Select", "0"));
//this line use for inserting "Select" in to list .
    }

step 3:
Call this function in to page load Event .
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
            FillClass();
          
        }
    }


Comments