Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi iam net to asp.net and i want to get selected data was saved to dropdownlist to edit it, i retrieve data to all controls ok but can't get in dropdownlist when i use
C#
DropDownList.SelectedItem.Text= dr["FiledName"].ToString();


it show in dropdownlist duplicate and when i use
C#
DropDownList.Items.Add(dr.GetString(0));

it doesn't show any thing but duplicate data in dropdownlist
Posted
Updated 14-Apr-13 22:04pm
v2

Try this. May help. good way to find the item in the list and if found set it selected.

SQL
if(DropDownList1.Items.FindByText(dr["FiledName"].ToString())!=null)
{
                DropDownList1.Items.FindByText(dr["FiledName"].ToString()).Selected = true;

}



~Prashant
 
Share this answer
 
v2
Comments
M.Saied 15-Apr-13 5:31am    
get error

Cannot have multiple items selected in a DropDownList
khalid92 15-Apr-13 5:38am    
which error you are still getting ...?
if you have duplicate field names than try to set selected index by its value like this

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(dr["FiledName"].ToString()));
M.Saied 16-Apr-13 0:58am    
thank you khalid it works
PrashantSonewane 15-Apr-13 6:38am    
if(DropDownList1.Items.FindByText(dr["FiledName"].ToString())!=null)
{
DropDownList1.ClearSelection();
DropDownList1.Items.FindByText(dr["FiledName"].ToString()).Selected = true;

}
M.Saied 16-Apr-13 1:01am    
thanks this also works
try this

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(dr["FiledName"].ToString()));
 
Share this answer
 
Comments
M.Saied 15-Apr-13 5:31am    
thank you khalid it works

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