0

I have a list (WayBill) with column(ProdyctType). I need to get these items and input then into dropDownList.

This is my columnType in SP07:

enter image description here

This is my code:

  protected override void CreateChildControls()
        {
            Label lblProducType = new Label();
            lblProducType.Text = "ProducType";
            DropDownList ddProducType = new DropDownList();
            ddProducType.Items.Add("-- Choose ProducType --");
            ddProducType.Width = 300;

            using (SPSite site = new SPSite("http://sf-spsdev02/TestSite/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList wayList = web.Lists["WayBill"];

                    //maybe use SPField class HERE ?

                }
            }


            lblProject = new Label();
            lblProject.Text = "Project";
            ddProject = new DropDownList();
            ddProject.Width = 300;
            ddProject.Items.Add("-- Choose project --");
            GetProject();
1
  • You have ProductType in your screenshot, ProducType in your code, and ProdyctType in your text above. Which spelling are you using?
    – Mike
    Apr 5, 2016 at 14:34

1 Answer 1

0

This answer is basing off your original question's code

...

 using (SPSite site = new SPSite("http://sf-spsdev02/TestSite/"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                web.AllowUnsafeUpdates = true;
                SPList wayList = web.Lists["WayBill"];

                SPFieldChoice field = (SPFieldChoice)wayList.Fields["ProducType"];
                foreach (string choice in field.Choices)
                {
                   ddProducType.Items.Add(choice);
                }

            }
        }
6
  • Give me this error : Value does not fall within the expected range.
    – Goshky
    Apr 5, 2016 at 14:04
  • on which line? You may have just mispelled the "ProductType", which I did too, following your original post...try my edit
    – Mike
    Apr 5, 2016 at 14:07
  • Not working, After comment this code, Web Part is working...
    – Goshky
    Apr 5, 2016 at 14:24
  • can you provide more information than that? What is the webpart doing? Which line does it error on? Did you change the field name? check all your variables. Try my edit...
    – Mike
    Apr 5, 2016 at 14:30
  • Where is your edit? Code is the same..
    – Goshky
    Apr 5, 2016 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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