Isaac
asked on
Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.
I have a webpart that I deployed but when I run it, I get the attached Stack Trace:
Below is the code
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));
}
}
stackTrace.txt
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/
Bind it this way.
http://nishantrana.wordpress.com/2009/03/23/using-spgridview-to-bound-to-list-data-in-sharepoint/
ASKER
wdosonjos,
That did not work. I get this error:
Error 1 Cannot implicitly convert type 'Microsoft.SharePoint.SPLi stItemColl ection' to 'Microsoft.SharePoint.SPLi st' C:\Users\sogunroI\Document s\Visual Studio 2008\Projects\HotWebPartLi ne\HotWebP artLine\We bPartCode\ HotWebPart Line.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.
That did not work. I get this error:
Error 1 Cannot implicitly convert type 'Microsoft.SharePoint.SPLi
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));
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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,M issingReuq uiredField s,etc.
set AutoGenerateColumns="false " in aspx of the gridview
ASKER
it is already false...
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.
Also, I'm not sure how it displayed the extra columns if AutoGenerateColumns is set to false.
ASKER
Yes, it still displayed the extra columns
Please post the definition of your GridView.
ASKER
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