Click here to Skip to main content
16,017,833 members
Please Sign up or sign in to vote.
3.60/5 (4 votes)
i want know
how i can bind a data from database into a dropdownlist with C# code
i want select productID and ProductName from table(product
and bind to dropdownlist
show ProductName in items
and prodictId for Datekeyfield
only i know should use a sqldatasource
Posted
Updated 7-Nov-17 8:14am

C#
cmd.CommandText = " Select * From GetUsersID";
        cmd.Connection = conn;
        conn.Open();
        DataTable dt  = new DataTable();

        dt.Load(cmd.ExecuteReader());
        conn.Close();

        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind(); 


Or Go to Link :-
http://www.dotnetspider.com/resources/7644-Bind-DropDownList-from-DataBase-Using-c.aspx[^]
 
Share this answer
 
v2
Comments
sspanda 22-Oct-12 21:59pm    
Thxx for this solution
pankajgarg1986 16-Nov-12 6:51am    
how to hide ddl item in code behind..ddl fill form the database
ashwini gaikwad 2021 26-Oct-23 3:03am    
i also did same but when the DataBind() gets execute the selected index becomes -1 to zero and first item gets selected if i try to update selected index to -1 gain its not getting changed what can be the reasons ?
C#
dropdownlistid.DataSource = ds.Tables[0];
dropdownlistid.DisplayMember = "Your Display Column Name from Data Table";
dropdownlistid.ValueMember= "Your Value Column Name from DataTable";
dropdownlistid.DataBind();

or go to this site Click Here
 
Share this answer
 
Comments
sadegh_rusta 29-Jun-12 10:39am    
oki want with selected a data of dropdownlist
drop downlist give me text or value of that row(or index) of selected item now
Tbone1983 29-Jun-12 11:09am    
I'm sorry but I don't understand your question/comment. If you would like the selected item then you will have to hook on the selected index changed event.
If it's something else you mean please explain.
nutheti9 10-Aug-12 3:34am    
how to get id of the dropdowmlist id inside listview. directly we can't bind the data base to dropdown of listview.thats why i need the dropdown id in side listview.
Hi,
You can bind dropdownlist by using these ways.
1. By using ListItemCollection.
2. By using Datatable.
3. By using Dictionary.


In your Aspx

ASP.NET
<div>
 <asp:dropdownlist id="drpDownNames" runat="server" xmlns:asp="#unknown">
 </asp:dropdownlist>
</div>


In Your Aspx code behind file

C#
ListItemCollection collection = new ListItemCollection();
collection.Add(new ListItem("Suits"));
collection.Add(new ListItem("Harvey Spector"));
collection.Add(new ListItem("Jessica Pearson"));
collection.Add(new ListItem("Mike Ross"));
collection.Add(new ListItem("Donna Paulson"));
collection.Add(new ListItem("Rachel"));
collection.Add(new ListItem("Travis Tanner"));

//Pass ListItemCollection as datasource
drpDownNames.DataSource = collection;
drpDownNames.DataBind();


2. By Using Datatable

C#
//Create DataTable this can be from database also.
DataTable dtName = new DataTable();

//Add Columns to Table
dtName.Columns.Add(new DataColumn("DisplayMember"));
dtName.Columns.Add(new DataColumn("ValueMember"));

//Now Add Values
dtName.Rows.Add("Suits","0");
dtName.Rows.Add("Harvey Spector","1");
dtName.Rows.Add("Jessica Pearson","2");
dtName.Rows.Add("Mike Ross","3");
dtName.Rows.Add("Donna Paulson","4");
dtName.Rows.Add("Rachel","5");

//At Last Bind datatable to dropdown.
drpDownNames.DataSource  = dtName;
drpDownNames.DataTextField = dtName.Columns["DisplayMember"].ToString();
drpDownNames.DataValueField = dtName.Columns["ValueMember"].ToString();
drpDownNames.DataBind();


3. By Using Dictionary

C#
Dictionary<string,> namesCollection = new Dictionary<string,>();

namesCollection.Add("Suits", "0");
namesCollection.Add("Harvey Spector", "1");
namesCollection.Add("Jessica Pearson", "2");
namesCollection.Add("Mike Ross", "3");
namesCollection.Add("Donna Paulson", "4");
namesCollection.Add("Rachel", "5");

drpDownNames.DataSource = namesCollection;
drpDownNames.DataTextField = "Key";
drpDownNames.DataValueField = "Value";
drpDownNames.DataBind();


As we can see if we use datatable or Dictionary, explicitly we need to mention datavaluefield and display member, but in case of ListItemCollection we do not need to set these two properties.
Ultimately, this is your choice which ever makes you more understandable and easy to implement.

Hope this helps.
 
Share this answer
 
Comments
Member 10286433 17-Oct-13 5:26am    
namesCollection.Add("Suits", "0");
namesCollection.Add("Harvey Spector", "1");
namesCollection.Add("Jessica Pearson", "2");
namesCollection.Add("Mike Ross", "3");




























































namesCollection.Add("Donna Paulson", "4");
namesCollection.Add("Rachel", "5");

drpDownNames.DataSource = namesCollection;
drpDownNames.DataTextField = "Key";
drpDownNames.DataValueField = "Value";
drpDownNames.DataBind();
nmhieu252 30-Aug-15 3:32am    
ASasAS
Member 10286433 17-Oct-13 5:27am    
i want know
how i can bind a data from database into a dropdownlist with C# code
i want select productID and ProductName from table(product
and bind to dropdownlist
show ProductName in items
and prodictId for Datekeyfield
only i know should use a sqldatasource
bitofweb 17-Oct-13 5:38am    
Check out this link: http://www.java2s.com/Tutorial/ASP.NET/0360__ADO.net-Database/LinkaspDropDownListwithaspSqlDataSource.htm
Although, you can do this through c# code, which much cleaner and easier way.

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