Click here to Skip to main content
12,400,183 members (46,900 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET C#4.0
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
DropDownList.SelectedItem.Text= dr["FiledName"].ToString();

it show in dropdownlist duplicate and when i use
DropDownList.Items.Add(dr.GetString(0));
it doesn't show any thing but duplicate data in dropdownlist
Posted 14-Apr-13 21:53pm
M.Saied2.3K
Updated 14-Apr-13 22:04pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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


~Prashant
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

try this

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(dr["FiledName"].ToString()));
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2016
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