Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Here is my list with name "Department" and name,mobile these are my column name and i want to add to my dropdown list only mobile number(column)plz help me ...

    SPSite site = new SPSite("http://alluser/Staffs");
    SPWeb web = site.OpenWeb();
    SPList list = web.Lists["Department"];
    SPFieldChoice filed = (SPFieldChoice)list.Fields["mobile"];
    drop_dept.DataSource = filed.Choices;
    drop_dept.DataBind();

that code not showing any thing to my dropdownlist.... can any one gave me sol.

share|improve this question

3 Answers 3

I used the same Code and it worked for me. Check your Choice Field I think You Might forgot to add Choices in your Choice Field.

I used Code from Shobha's Blog and it is also working.

share|improve this answer

you can try this:

        Guid siteId = SPContext.Current.Site.ID;
        SPSecurity.RunWithElevatedPrivileges(delegate()
         {
             using (SPSite site = new SPSite(siteId))
             {
                 using (SPWeb objSPWeb = site.RootWeb)
                 {
                     SPList objSPList = objSPWeb.Lists["Department"];

                     SPFieldChoice field = (SPFieldChoice)objSPList.Fields["mobile"];
                     DropDownList1.DataSource = field.Choices;
                     DropDownList1.DataBind();
                 }
             }
         });

very similar to yours, as i think yours work, maybe its the user your using? so iv wrapped it into a runwithelevatedpriv

hope it helps :)

share|improve this answer
1  
I dont know why iv been marked down as its perfectly fine to wrap a spsecurity as the user the op could be using could not have access to the list that they want to get the mobile colum from! –  ali Sharepoint Dec 29 '12 at 11:03
    
thanks......its working –  panwar Dec 31 '12 at 6:03
    
if my answer is correct please mark it as correct so others know the answer two when they come up with the same issue :) thanks –  ali Sharepoint Dec 31 '12 at 23:38

I found that for some reason the following code:

drop_dept.DataSource = filed.Choices;
drop_dept.DataBind();

always makes drop_dept.SelectedItem refer to the first element on the list regardless the item actually selected.

Replacing it with:

foreach (string choice in filed.Choices)
{
    drop_dept.Items.Add(choice);
}

solved my problem.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.