Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP ASP.NET WebForms C#4.0 , +
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 29-Jun-12 5:13am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
v2
Comments
sspanda at 22-Oct-12 21:59pm
   
Thxx for this solution
pankajgarg1986 at 16-Nov-12 6:51am
   
how to hide ddl item in code behind..ddl fill form the database
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
sadegh_rusta at 29-Jun-12 10:39am
   
ok
i want with selected a data of dropdownlist
drop downlist give me text or value of that row(or index) of selected item now
Tbone1983 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi,
You can bind dropdownlist by using these ways.
1. By using ListItemCollection.
2. By using Datatable.
3. By using Dictionary.
 

In your Aspx
 
<div>
 <asp:dropdownlist id="drpDownNames" runat="server" xmlns:asp="#unknown">
 </asp:dropdownlist>
</div>
 
In Your Aspx code behind file
 
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
 
//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
 
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.
  Permalink  
Comments
Member 10286433 at 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();
Member 10286433 at 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
abhidev_sj at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

zxvvvi 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 sqldatasourcexvvxvxvx
  Permalink  
v2
Comments
André Kraak at 17-Oct-13 5:33am
   
You posted a question as an solution which was probably not what you intended.
 
If you have a question or comment about the original question use the "Have a Question or Comment?" option beneath the question.
On the other hand if you have a similar problem your best option is to create your own new question, for this use the "Ask a Question" button at the top of the page.
 
Please move the content of this solution to the appropriate section and remove the solution.
 
Thank you.

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

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 17 Oct 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100