Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, All

Here my prob is, i insert male and female in dropdown list and how can i retrive the data from DB

For Ex:

SQL
ddlgender.Items.Insert(0, new ListItem("--Select--", ""));
           ddlgender.Items.Insert(1, new ListItem("Male", "1"));
           ddlgender.Items.Insert(2, new ListItem("FeMale", "2"));


i insert this item to DDL when user save to DB how can i retrieve that item in dropdown list, if user save male item means i want show female also but male want to selected item=true;
Posted
Comments
Ashi0891 20-Aug-14 4:51am    
lemme try to understand you want to save the selected drop downlist item into database for respective entry?
Ashi0891 20-Aug-14 5:28am    
From what i could get, you want to add selected data from dropdownlist into database to respective entry. And at the time when you show the data you want to show all the items of dropdownlist and the one which was selected and is added in the table data should be selected or be highlighted. Is it so?
prasanna.raj 20-Aug-14 6:58am    
yes..

You can use SelectedIndex property..

C#
ddlgender.SelectedIndex=1 // for Male
ddlgender.SelectedIndex=2 // for Female
 
Share this answer
 
v2
Comments
prasanna.raj 20-Aug-14 5:11am    
how we know index number which they selected...
Gupta Poonam 20-Aug-14 5:16am    
int index= ddlgender.SelectedIndex // To get SelectedIndex
int value=(Convert.ToInt32(ddlgender.SelectedValue)) // To get SelectedValue
prasanna.raj 20-Aug-14 6:58am    
thanks Gupta..
Hello,

As per your problem,

1. User selects the gender
2. The selected value gets stored in the database.
3. Now if you want to :

- Display the dropdownlist with the selected value:
ddlGender.Items.FindByText(<here put="" the="" value="" coming="" from="" database="">).Selected = true;</here>


- If you want to get the selected text:
ddlGender.SelectedItem.Text


- If you want to get the selected value:
ddlGender.SelectedItem.Value


- If you want to get the selected index:
ddlGender.SelectedIndex


Hope this helps you :)
 
Share this answer
 
Comments
prasanna.raj 20-Aug-14 7:29am    
super...
You can try this.
SQL
ddlgender.SelectedIndex =
    ddlgender.Items.IndexOf(ddlgender.Items.FindByValue("Male"))
 
Share this answer
 
Comments
prasanna.raj 20-Aug-14 6:58am    
thanks Kumarbs
Kumarbs 20-Aug-14 7:06am    
If this solves your prob accept this as a solution.

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