locked
How to take DropDownList source from database? RRS feed

  • Question

  • User505252109 posted
    Hi giys, I'm studying vs 2008 and encountered some problem..
    I need to take DDL list from database but I don't know how to do it. Could you guys help me please?

    Thank you in advance
    Thursday, January 8, 2015 2:27 AM

Answers

  • User1577371250 posted
    1. Add this to your aspx
    
    <asp:DropDownList ID="ddlCountry" runat="server" />
    2. In Page Load Bind the DropDownList
    
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
       {
          BindDropDownList();
       }
    }
    3. Get the data from the database
    
    
    protected void BindDropDownList()
    {
        //conenction to database
       using (SqlConnection con = new SqlConnection("Data Source=SQLEXPRESS;Integrated Security=true;Initial Catalog=EmployeeDB"))
       {
         con.Open();
         SqlCommand cmd = new SqlCommand("Select Id,Name FROM Countries", con);
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         DataSet ds = new DataSet();
         da.Fill(ds);
         ddlCountry.DataSource = ds;
         ddlCountry.DataTextField = "Name"; // Display Field in DropdownList
         ddlCountry.DataValueField = "Id"; // Value Field in DropdownList
         ddlCountry.DataBind();
         ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
         con.Close();
       }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2015 2:46 AM
  • User61956409 posted

    Hi dzakyalfajr,

    Thanks for your post.

    Firstly, as Lokesh said, you could bind a data source to your DropDownList from code behind.

    Secondly, you could also set the DataSourceID property of the DropDownList to the ID value of the data source control. The DropDownList automatically binds to the specified data source control as below.

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Id" DataValueField="Name"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [YourTable]"></asp:SqlDataSource>
    

    Hope it will be helpful for you.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2015 9:26 PM

All replies

  • User1577371250 posted
    1. Add this to your aspx
    
    <asp:DropDownList ID="ddlCountry" runat="server" />
    2. In Page Load Bind the DropDownList
    
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
       {
          BindDropDownList();
       }
    }
    3. Get the data from the database
    
    
    protected void BindDropDownList()
    {
        //conenction to database
       using (SqlConnection con = new SqlConnection("Data Source=SQLEXPRESS;Integrated Security=true;Initial Catalog=EmployeeDB"))
       {
         con.Open();
         SqlCommand cmd = new SqlCommand("Select Id,Name FROM Countries", con);
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         DataSet ds = new DataSet();
         da.Fill(ds);
         ddlCountry.DataSource = ds;
         ddlCountry.DataTextField = "Name"; // Display Field in DropdownList
         ddlCountry.DataValueField = "Id"; // Value Field in DropdownList
         ddlCountry.DataBind();
         ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
         con.Close();
       }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2015 2:46 AM
  • User61956409 posted

    Hi dzakyalfajr,

    Thanks for your post.

    Firstly, as Lokesh said, you could bind a data source to your DropDownList from code behind.

    Secondly, you could also set the DataSourceID property of the DropDownList to the ID value of the data source control. The DropDownList automatically binds to the specified data source control as below.

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Id" DataValueField="Name"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [YourTable]"></asp:SqlDataSource>
    

    Hope it will be helpful for you.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2015 9:26 PM