locked
Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. RRS feed

  • Question

  • what am I missing here?

    using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;

    namespace PurchasingPortal.Vendor.DropDown
    {
        [ToolboxItemAttribute(false)]
        public class DropDown : WebPart
        {
            String drpListSelected = "";
            DropDownList drpList = new DropDownList();

            protected override void CreateChildControls()
            {
                SPSite site = SPContext.Current.Site;
                SPWeb web = SPContext.Current.Web;

                SPList list1 = web.Lists["Vendor"];
                var listitems = list1.Fields["Company"];
                drpList.DataSource = listitems;
                drpList.DataTextField = "Company";
                drpList.DataValueField = "ID";
                drpList.DataBind();
                Controls.Add(drpList);

                drpList.SelectedIndexChanged += new EventHandler(this.ddlAllLists_SelectedIndexChanged);
            }

            void ddlAllLists_SelectedIndexChanged(object sender, EventArgs e)
            {
                try
                {
                    //set the selected value and logic
                    drpListSelected = drpList.SelectedValue;
                    this.Page.Response.Redirect("http://drive.penskeautomotive.com/corporateservices/purchasing/Lists/Vendor/DispFormVendor.aspx?iD=" + drpListSelected);

                }
                catch (Exception ex)
                {
                    //  LogManger.WriteLog(" Error in EventCalendarWebPart.cs ddlAllLists_SelectedIndexChanged()" + ex.ToString());
                }
            }
            protected override void Render(HtmlTextWriter output)
            {
                EnsureChildControls();
                base.RenderContents(output);
            }
        }
    }

    Friday, June 27, 2014 2:26 PM

Answers

  • Does this work?

    protected override void CreateChildControls()
    {
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
    	SPQuery query = new SPQuery();
    	SPList list1 = web.Lists.TryGetList("Vendor");
    	if list1 != null)
    	{
            	DataTable dtvendor = new DataTable();
    		dtvendor = list1.GetItems(query).GetDataTable();
                 	drpList.DataSource = dtvendor;
                 	drpList.DataTextField = "Company";
                 	drpList.DataValueField = "ID";
                 	drpList.DataBind();
                 	Controls.Add(drpList);
    		drpList.SelectedIndexChanged += new EventHandler(this.ddlAllLists_SelectedIndexChanged);
             }
    }
    

    Good Luck!


    Alex

    • Marked as answer by DavidLewis27 Monday, June 30, 2014 12:02 PM
    Friday, June 27, 2014 7:38 PM

All replies

  • Does this work?

    protected override void CreateChildControls()
    {
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
    	SPQuery query = new SPQuery();
    	SPList list1 = web.Lists.TryGetList("Vendor");
    	if list1 != null)
    	{
            	DataTable dtvendor = new DataTable();
    		dtvendor = list1.GetItems(query).GetDataTable();
                 	drpList.DataSource = dtvendor;
                 	drpList.DataTextField = "Company";
                 	drpList.DataValueField = "ID";
                 	drpList.DataBind();
                 	Controls.Add(drpList);
    		drpList.SelectedIndexChanged += new EventHandler(this.ddlAllLists_SelectedIndexChanged);
             }
    }
    

    Good Luck!


    Alex

    • Marked as answer by DavidLewis27 Monday, June 30, 2014 12:02 PM
    Friday, June 27, 2014 7:38 PM
  • You should directly assigned the field to dropdownlist data source

        drpList.DataSource = listitems; //This line is wrong in your code

    You should bind the list items like this

    using (SPSite site = new SPSite("http://devserver/somesite")) 
    { 
        using (SPWeb web = site.OpenWeb()) 
        { 
            SPList list = web.Lists["Departments"]; 
            cboDepartment.DataSource = list.Items; 
            cboDepartment.DataValueField = "Title"; // List field holding value
            cboDepartment.DataTextField = "Title"; // List field holding name to be displayed on page 
            cboDepartment.DataBind(); 
        } 
    }

    If you have a choice column then bind like this

    http://sharepoint.stackexchange.com/questions/54877/how-to-bind-dropdownlist-from-splist-column-with-choice-field-using-c

    • Edited by sk2014 Monday, June 30, 2014 8:02 AM
    Monday, June 30, 2014 8:01 AM
  • thanks Alex.  that was money!!!
    Monday, June 30, 2014 12:02 PM