Wednesday, 25 July 2012

Binding Dropdownlist with dataset values in ASP.NET


Binding Dropdownlist with dataset values in ASP.NET

    Suppose you have a dropdown list in you aspx page.
   
   <asp:DropDownList ID="drpbranch" runat="server" Width="350px"></asp:DropDownList>


   Now we bind this dropdownlist with dataset values.
   Suppose we have a table in our SQL database names Branch having fileds BR_CD and BR_NM.


   Now. in aspx.cs page.


   private void FillDropDownList()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);


        SqlCommand cmd = new SqlCommand("Select * from Branch", con);


        SqlDataAdapter da = new SqlDataAdapter(cmd);


        DataSet ds = new DataSet();
        da.Fill(ds);
       
         drpbranch .DataTextField = ds.Tables[0].Columns[" BR_NM "].ToString();
         drpbranch .DataValueField = ds.Tables[0].Columns[" BR_CD "].ToString();


         drpbranch .DataSource = ds.Tables[0];
         drpbranch .DataBind();
    }


   This post might help you.


  Thanks





1 comment:

  1. Short and sweet code......found a bit different one here
    http://sharepoint-2010-world.blogspot.in/2013/11/bind-dropdownlist-with-database-values.html

    ReplyDelete