-->

Sunday, April 13, 2014

How to bind Dropdownlist Control in ASP.NET

How to bind Dropdownlist Control in ASP.NET

In the previous example we have already discussed about  DropdownList like .

  1. How to change font name at runtime
  2. Bind DropdownList using XML file 

For binding DropdownList using ado.net you must use System.Data.SqlClient namespace, which is contains various classes like SqlConnection,SqlCommand etc.



<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            con.Open();
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandText = "insrtdata";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Connection = con;
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
            System.Data.DataSet ds = new System.Data.DataSet();
            da.Fill(ds);
            DropDownList1 .DataTextField ="Name";
        DropDownList1 .DataValueField  ="Id";
        DropDownList1 .DataSource =ds;
        DropDownList1 .DataBind ();        
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>  
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>  
    </div>
    </form>
</body>
</html>

Output
How to bind dropdownlist in asp.net

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved