Solved

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

Posted on 2011-03-18
11
3,300 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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