Click here to Skip to main content
15,886,797 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hi Experts !!

I have a dropdownlist in my aspx page.

I want to take the data from database and place in this dropdownlist. whatever the data is in table it must display in dropdown.

and when ever i add new item in table in should display in dropdownlist.

Please can you help me, how to do this.

Thanks.
Posted

C#
protected void Page_Load(object sender, EventArgs e)
        {
If(!IsPostBack)
  BindDropDownList();
}

private void BindDropDownList()
{
   DataTable dtData = new DataTable();
   dtData = //GetDataFromDb();
   this.Ddl.DataSource = dtData;
   this.Ddl.DataValueField = "ID";
   this.Ddl.DataTextField = "Name";
   this.Ddl.DataBind();
}

protected void BtnAdd_Click(object sender, EventArgs e)
{
    // Add data in DB
   BindDropDownList();
}


Please follow this link

http://stackoverflow.com/questions/4242766/binding-a-dropdownlist-to-a-database[^][]
 
Share this answer
 
v3
Comments
Ranjith Reddy CSE 24-Feb-13 2:21am    
No need of Sql Connections here.....Why ?
Asim Mahmood 24-Feb-13 2:24am    
this method is showing GetDataFromDb(); you'll get data from you DataBase like BLL.GetDataFromDb();. if you dont have idea how to build SQLConnection it's another point.
Several ways to do this:

you can write sql command that pull the data from the database as

SqlCommand cmd = new SqlCommand("Select Value1,Value2 from table");

And then specify the datasource of your dropdownlist as follows:
dropdownlist1.datasource() cmd.executereader();

dropdownlist1.datatextfield = "Value1" -- this means Value1 is what you want to show in the drop down

dropdownlist1.databind(); -- this what will do the final binding when you run the page.

There more resources and difference ways. Feel free to read more about different ways of doing things.
 
Share this answer
 
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Dropdownlist_Insertshow._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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Name" Width="100px"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Button ID="btn_add" runat="server" Text="Add Name" onclick="btn_add_Click"
            Width="100px" /><br />
    <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList><br />

    </div>
    </form>
</body>
</html>




using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Dropdownlist_Insertshow
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dropbind();
}
}

protected void btn_add_Click(object sender, EventArgs e)
{
DropDownList1.Items.Clear();
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "Insert into employee values('" + TextBox1.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
Response.Write("Records successfully inserted");
clear();
dropbind();
}
private void clear()
{
TextBox1.Text = "";
}
private void dropbind()
{

SqlConnection con = new SqlConnection(strConnString);
DropDownList1.Items.Add("Choose Name");
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["name"].ToString());
}
reader.Close();
con.Close();
}

}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900