Solved

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

Posted on 2011-03-18
11
3,343 Views
Last Modified: 2012-05-11
I have a webpart that I deployed but when I run it, I get the attached Stack Trace:

Below is the code
{
        private bool _error = false;
        private string _myProperty = null;

        SPSite mySite = SPContext.Current.Site;
        SPWeb myWeb;
        SPList myList;

        GridView myGridView = new GridView();
        
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello SharePoint";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        }


        public WebPartFeature1()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {

                    base.CreateChildControls();

                    myWeb = SPContext.Current.Web;
                    myList = myWeb.Lists["HOTLINE"];
                    SPListItemCollection results = myList.Items;

                    //Create the datatable object
                    DataTable table;
                    table = new DataTable();
                    table.Columns.Add("Type", typeof(string));
                    table.Columns.Add("Name", typeof(string));
                    table.Columns.Add("Modified", typeof(string));
                    table.Columns.Add("Modified By", typeof(string));
                    table.Columns.Add("STATUS", typeof(string));
                    table.Columns.Add("PRIORITY CODE", typeof(string));
                    table.Columns.Add("PRIORITY", typeof(string));
                    table.Columns.Add("CASE NUMBER", typeof(string));
                    table.Columns.Add("COMPLETE", typeof(string));

                    //Create the rows for each splistitem
                    DataRow row;
                    foreach (SPListItem result in results)
                    {
                        row = table.Rows.Add();
                        row["Type"] = result["Type"].ToString();
                        row["Name"] = result["Name"].ToString();
                        row["Modified"] = result["Modified"].ToString();
                        row["Modified By"] = result["Modified By"].ToString();
                        row["STATUS"] = result["STATUS"].ToString();
                        row["PRIORITY CODE"] = result["PRIORITY CODE"].ToString();
                        row["PRIORITY"] = result["PRIORITY"].ToString();
                        row["CASE NUMBER"] = result["CASE NUMBER"].ToString();
                        row["COMPLETE"] = result["COMPLETE"].ToString();
                    }

                    myGridView.AutoGenerateColumns = false;
                    myGridView.AllowPaging = true;
                    myGridView.PageSize = 100;

                    BoundField HL_TYPE = new BoundField();
                    HL_TYPE.HeaderText = "TYPE";
                    HL_TYPE.DataField = "TYPE";
                    HL_TYPE.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_TYPE.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_TYPE);

                    BoundField HL_NAME = new BoundField();
                    HL_NAME.HeaderText = "NAME";
                    HL_NAME.DataField = "NAME";
                    HL_NAME.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_NAME.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_NAME);

                    BoundField HL_MODIFIED = new BoundField();
                    HL_MODIFIED.HeaderText = "MODIFIED";
                    HL_MODIFIED.DataField = "MODIFIED";
                    HL_MODIFIED.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_MODIFIED.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_MODIFIED);

                    BoundField HL_MODIFIED_BY = new BoundField();
                    HL_MODIFIED_BY.HeaderText = "MODIFIED BY";
                    HL_MODIFIED_BY.DataField = "MODIFIED BY";
                    HL_MODIFIED_BY.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_MODIFIED_BY.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_MODIFIED_BY);

                    BoundField HL_STATUS = new BoundField();
                    HL_STATUS.HeaderText = "STATUS";
                    HL_STATUS.DataField = "STATUS";
                    HL_STATUS.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_STATUS.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_STATUS);

                    BoundField HL_PRIORITY_CODE = new BoundField();
                    HL_PRIORITY_CODE.HeaderText = "PRIORITY CODE";
                    HL_PRIORITY_CODE.DataField = "PRIORITY CODE";
                    HL_PRIORITY_CODE.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_PRIORITY_CODE.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_PRIORITY_CODE);

                    BoundField HL_PRIORITY = new BoundField();
                    HL_PRIORITY.HeaderText = "PRIORITY";
                    HL_PRIORITY.DataField = "PRIORITY";
                    HL_PRIORITY.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_PRIORITY.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_PRIORITY);

                    BoundField HL_CASE_NUMBER = new BoundField();
                    HL_CASE_NUMBER.HeaderText = "CASE NUMBER";
                    HL_CASE_NUMBER.DataField = "CASE NUMBER";
                    HL_CASE_NUMBER.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_CASE_NUMBER.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_CASE_NUMBER);

                    BoundField HL_COMPLETE = new BoundField();
                    HL_COMPLETE.HeaderText = "COMPLETE";
                    HL_COMPLETE.DataField = "COMPLETE";
                    HL_COMPLETE.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_COMPLETE.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_COMPLETE);


                    // Your code here...
                    this.Controls.Add(new LiteralControl(this.MyProperty));
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            SPSite mySite = SPContext.Current.Site;
            SPWeb myWeb = SPContext.Current.Web;
            SPList myList = myWeb.Lists["HOTLINE"];

            myGridView.DataSource = myList;
            //spDataSource.List = myList;
            //SPGridView1.DataSource = spDataSource;
            myGridView.DataBind();
            //SPGridView1.DataBind();
            myGridView.RenderControl(writer);
            //SPGridView1.RenderControl(writer);
        }
        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }

Open in new window

stackTrace.txt
0
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35167882
I think it should be myGridView.DataSource = myList.Items; in Render, as follows:

        protected override void Render(HtmlTextWriter writer)
        {
            SPSite mySite = SPContext.Current.Site;
            SPWeb myWeb = SPContext.Current.Web;
            SPList myList = myWeb.Lists["HOTLINE"];

            myGridView.DataSource = myList.Items;
            //spDataSource.List = myList;
            //SPGridView1.DataSource = spDataSource;
            myGridView.DataBind();
            //SPGridView1.DataBind();
            myGridView.RenderControl(writer);
            //SPGridView1.RenderControl(writer);
        }

Open in new window

0
 
LVL 4

Expert Comment

by:abhinayp
ID: 35167900
U cant actually directly use SPList as ur gridview datasource (in line:187).

Bind it this way.

http://nishantrana.wordpress.com/2009/03/23/using-spgridview-to-bound-to-list-data-in-sharepoint/
0
 
LVL 5

Author Comment

by:Isaac
ID: 35191249
wdosonjos,
That did not work.  I get this error:
Error      1      Cannot implicitly convert type 'Microsoft.SharePoint.SPListItemCollection' to 'Microsoft.SharePoint.SPList'      C:\Users\sogunroI\Documents\Visual Studio 2008\Projects\HotWebPartLine\HotWebPartLine\WebPartCode\HotWebPartLine.cs      209      33      HotWebPartLine


abhinayp,
That's what I tried first but then I got this error: Value does not fall within the expected range.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace HotWebPartLine
{
    [Guid("a51d67cf-bcba-4414-b180-ff90165a4169")]
    public class HotWebPartLine : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myProperty = null;

        SPSite mySite = SPContext.Current.Site;
        SPWeb myWeb;
        SPList myList;

        SPGridView SPGridView1 = new SPGridView();
        SPDataSource spDataSource = new SPDataSource();


        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello SharePoint";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        }


        public HotWebPartLine()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {

                    base.CreateChildControls();
                    
                    myWeb = SPContext.Current.Web;

                    //myWeb = mySite.OpenWeb();

                    myList = myWeb.Lists["Hotline List"];

                    SPListItemCollection results = myList.Items;

 

                    //Create the datatable object

                    DataTable table;

                    table = new DataTable();

                    table.Columns.Add("Type", typeof(string));
                    table.Columns.Add("Case Number", typeof(string));
                    table.Columns.Add("Name", typeof(string));
                    table.Columns.Add("Modified", typeof(string));
                    table.Columns.Add("Modified By", typeof(string));
                    table.Columns.Add("Status", typeof(string));
                    table.Columns.Add("Priority Code", typeof(string));
                    table.Columns.Add("Priority", typeof(string));
                    table.Columns.Add("COMPLETE", typeof(string));

 

                    //Create the rows for each splistitem

                    DataRow row;

                    foreach (SPListItem result in results)

                    {

                        row = table.NewRow();

                        row["Type"] = result["Type"].ToString();
                        row["Case Number"] = result["Case Number"].ToString();
                        row["Name"] = result["Name"].ToString();
                        row["Modified"] = result["Modified"].ToString();
                        row["Modified By"] = result["Modified By"].ToString();
                        row["Status"] = result["Status"].ToString();
                        row["Priority Code"] = result["Priority Code"].ToString();
                        row["Priority"] = result["Priority"].ToString();
                        row["COMPLETE"] = result["COMPLETE"].ToString();
                        table.Rows.Add(row);

                    }

 

                    //SPGridView1 = new SPGridView();

                    SPGridView1.Enabled = true;
                    SPGridView1.AutoGenerateColumns = false;
                    SPGridView1.ID = "hotView"; 

                    SPBoundField HL_TYPE = new SPBoundField();
                    HL_TYPE.HeaderText = "Type";
                    HL_TYPE.DataField = "Type";
                    HL_TYPE.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_TYPE.ItemStyle.Wrap = false;
                    SPGridView1.Columns.Add(HL_TYPE);

                    SPBoundField HL_CASE_NUMBER = new SPBoundField();
                    HL_CASE_NUMBER.HeaderText = "Case Number";
                    HL_CASE_NUMBER.DataField = "Case Number";
                    SPGridView1.Columns.Add(HL_CASE_NUMBER);

                    SPBoundField HL_NAME = new SPBoundField();
                    HL_NAME.HeaderText = "Name";
                    HL_NAME.DataField = "Name";
                    SPGridView1.Columns.Add(HL_NAME);
                    
                    SPBoundField HL_MODIFIED = new SPBoundField();
                    HL_MODIFIED.HeaderText = "Modified";
                    HL_MODIFIED.DataField = "Modified";
                    SPGridView1.Columns.Add(HL_MODIFIED);
                    
                    SPBoundField HL_MODIFIED_BY = new SPBoundField();
                    HL_MODIFIED_BY.HeaderText = "Modified By";
                    HL_MODIFIED_BY.DataField = "Modified By";
                    SPGridView1.Columns.Add(HL_MODIFIED_BY);

                    SPBoundField HL_STATUS = new SPBoundField();
                    HL_STATUS.HeaderText = "Status";
                    HL_STATUS.DataField = "Status";
                    SPGridView1.Columns.Add(HL_STATUS);

                    SPBoundField HL_PRIORITY_CODE = new SPBoundField();
                    HL_PRIORITY_CODE.HeaderText = "Priority Code";
                    HL_PRIORITY_CODE.DataField = "Priority Code";
                    SPGridView1.Columns.Add(HL_PRIORITY_CODE);

                    SPBoundField HL_PRIORITY = new SPBoundField();
                    HL_PRIORITY.HeaderText = "Priority";
                    HL_PRIORITY.DataField = "Priority";
                    SPGridView1.Columns.Add(HL_PRIORITY);

                    SPBoundField HL_COMPLETE = new SPBoundField();
                    HL_COMPLETE.HeaderText = "COMPLETE";
                    HL_COMPLETE.DataField = "COMPLETE";
                    SPGridView1.Columns.Add(HL_COMPLETE);

                    spDataSource.List = myList;
                    SPGridView1.DataSource = spDataSource;
                    SPGridView1.DataBind();

                    this.Controls.Add(new LiteralControl(this.MyProperty));
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            SPSite mySite = SPContext.Current.Site;
            SPWeb myWeb = SPContext.Current.Web;
            SPList myList = myWeb.Lists["Hotline List"];

            spDataSource.List = myList;
            SPGridView1.DataSource = spDataSource;
            SPGridView1.DataBind();

            SPGridView1.RenderControl(writer);

        }
        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35195791
Please try:

        protected override void Render(HtmlTextWriter writer)
        {
            SPSite mySite = SPContext.Current.Site;
            SPWeb myWeb = SPContext.Current.Web;
            SPList myList = myWeb.Lists["HOTLINE"];

            myGridView.DataSource = myList.Items.List;
            //spDataSource.List = myList;
            //SPGridView1.DataSource = spDataSource;
            myGridView.DataBind();
            //SPGridView1.DataBind();
            myGridView.RenderControl(writer);
            //SPGridView1.RenderControl(writer);
        }

Open in new window

0
 
LVL 5

Author Comment

by:Isaac
ID: 35202008
It rendered data in a gridview to the page but it had extra columns that I've never seen before nor did I create. Ex: Xml,uniquID,RecurrenceID,MissingReuquiredFields,etc.
0
 
LVL 4

Expert Comment

by:abhinayp
ID: 35210277
set AutoGenerateColumns="false" in aspx of the gridview
0
 
LVL 5

Author Comment

by:Isaac
ID: 35232347
it is already false...
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35244006
Did it display your data columns along with the extra columns?

Also, I'm not sure how it displayed the extra columns if AutoGenerateColumns is set to false.
0
 
LVL 5

Author Comment

by:Isaac
ID: 35262204
Yes, it still displayed the extra columns
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35284585
Please post the definition of your GridView.
0
 
LVL 5

Author Comment

by:Isaac
ID: 35295903
I did with my initial post or are you asking for something else.
myGridView.AutoGenerateColumns = false;
                    myGridView.AllowPaging = true;
                    myGridView.PageSize = 100;

                    BoundField HL_TYPE = new BoundField();
                    HL_TYPE.HeaderText = "TYPE";
                    HL_TYPE.DataField = "TYPE";
                    HL_TYPE.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_TYPE.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_TYPE);

                    BoundField HL_NAME = new BoundField();
                    HL_NAME.HeaderText = "NAME";
                    HL_NAME.DataField = "NAME";
                    HL_NAME.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_NAME.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_NAME);

                    BoundField HL_MODIFIED = new BoundField();
                    HL_MODIFIED.HeaderText = "MODIFIED";
                    HL_MODIFIED.DataField = "MODIFIED";
                    HL_MODIFIED.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_MODIFIED.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_MODIFIED);

                    BoundField HL_MODIFIED_BY = new BoundField();
                    HL_MODIFIED_BY.HeaderText = "MODIFIED BY";
                    HL_MODIFIED_BY.DataField = "MODIFIED BY";
                    HL_MODIFIED_BY.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_MODIFIED_BY.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_MODIFIED_BY);

                    BoundField HL_STATUS = new BoundField();
                    HL_STATUS.HeaderText = "STATUS";
                    HL_STATUS.DataField = "STATUS";
                    HL_STATUS.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_STATUS.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_STATUS);

                    BoundField HL_PRIORITY_CODE = new BoundField();
                    HL_PRIORITY_CODE.HeaderText = "PRIORITY CODE";
                    HL_PRIORITY_CODE.DataField = "PRIORITY CODE";
                    HL_PRIORITY_CODE.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_PRIORITY_CODE.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_PRIORITY_CODE);

                    BoundField HL_PRIORITY = new BoundField();
                    HL_PRIORITY.HeaderText = "PRIORITY";
                    HL_PRIORITY.DataField = "PRIORITY";
                    HL_PRIORITY.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_PRIORITY.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_PRIORITY);

                    BoundField HL_CASE_NUMBER = new BoundField();
                    HL_CASE_NUMBER.HeaderText = "CASE NUMBER";
                    HL_CASE_NUMBER.DataField = "CASE NUMBER";
                    HL_CASE_NUMBER.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_CASE_NUMBER.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_CASE_NUMBER);

                    BoundField HL_COMPLETE = new BoundField();
                    HL_COMPLETE.HeaderText = "COMPLETE";
                    HL_COMPLETE.DataField = "COMPLETE";
                    HL_COMPLETE.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    HL_COMPLETE.ItemStyle.Wrap = false;
                    myGridView.Columns.Add(HL_COMPLETE);

Open in new window

0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question