0

Please forgive me if I am horrible at explaining this. I have a project where I have standalone business objects. I'm trying to create an asp:listview with one of those business objects, but it keeps telling me that it's an invalid type, or not even displaying.

The business object logic is here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using WebStore.DatabaseHelper;
using System.Configuration;

namespace WebStore.BusinessObjectHelper
{
    public class ProductCategoryList
    {

        private BindingList<ProductCategory> _List = new BindingList<ProductCategory>();

        public BindingList<ProductCategory> List
        {
            get { return _List; }
        }

        public ProductCategoryList GetAll()
        {
            Database db = new Database(ConfigurationManager.AppSettings["store"]);
            db.Command.CommandType = System.Data.CommandType.StoredProcedure;
            db.Command.CommandText = "tblProductCategory_GetAll";
            DataSet ds = new DataSet();
            ds = db.ExecuteQuery();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ProductCategory pc = new ProductCategory();
                pc.Initialize(dr);
                pc.InitializeBusinessData(dr);
                pc.IsNew = false;
                pc.IsDirty = false;
                _List.Add(pc);
            }
            return this;
        }

        public ProductCategoryList Save()
        {
            for (int i = 0; i < _List.Count; i++)
            {
                if (_List[i].IsSavable())
                {
                    _List[i] = _List[i].Save();
                }
            }
            return this;
        }
    }
}

This is what I am trying to do:

<div id="CategoryMenu" style="text-align: center">
    <asp:ListView ID="categoryList" runat="server" ItemType="WebStore.BusinessObjectHelper.ProductCategoryList" SelectMethod="GetAll">
        <ItemTemplate>
            <b style="font-size: large; font-style: normal">
                <a href="ProductList.aspx?id=<%# Eval("ID") %>">
                    <%# Eval("name") %>
                </a>
            </b>
        </ItemTemplate>
        <ItemSeparatorTemplate>| </ItemSeparatorTemplate>
    </asp:ListView>
</div>

I get an error:

An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user code

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

In my code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        categoryList.DataSource = new ProductCategoryList();
        categoryList.DataBind();
    }

    public ProductCategoryList GetCategories()
    {
        var _db = new WebStore.BusinessObjectHelper.ProductCategoryList();
        ProductCategoryList query = _db.GetAll();
        return query;
    }

Am I not able to display this object in an asp:listview?

celerno
  • 1,367
  • 11
  • 30
Everex210
  • 3
  • 2
  • Have you tried other types of collections? I think we typically use regular List for our ListViews. – floppsb Aug 13 '14 at 15:03

2 Answers2

0

The error message...

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

... tells you what's wrong. That is the ListView class only knows how to "list" things that inherit/implement IListSource, IEnumerable, or IDataSource classes.

Your business object ProductCategoryList needs to implement one of those interfaces, so that it can be iterated by the ListView.

namespace WebStore.BusinessObjectHelper
{
    public class ProductCategoryList: IEnumerable<ProductCategory>
    {
        // *implement the IEnumerable interface
    }
}

see also: https://stackoverflow.com/a/6908510/1718702

Basically you are handing your custom class to the ListView (which has absolutely no clue what your class does) and expecting it to guess at what it should do with it (and it throws an exception). You need to implement one of those interfaces to say, "Here! This is how you should use me properly!".

Community
  • 1
  • 1
HodlDwon
  • 1,131
  • 1
  • 13
  • 30
0

This method is not returning a IEnumerable, or IDataSource. is returning a ProductCategoryList object type.

    public ProductCategoryList GetAll()
    {
     /*
          ...
     */
     return this;
    }

You could change the return type for your method GetAll() or implement an interface to ProductCategoryList in order to make it IEnumerable

celerno
  • 1,367
  • 11
  • 30