Help: Data Binding to Custom Collection

Discussion in 'ASP .Net' started by Shrage Smilowitz, Oct 30, 2003.

  1. I try to create a custom collection which i will then use to databind and
    its not working.

    Class Book
    Public BookName As String
    Sub New(ByVal lBookName As String)
    Me.BookName = BookName
    End Sub
    End Class

    Public Class books
    Inherits Collections.CollectionBase
    Default Property Item(ByVal index As Integer) As Book
    Get
    Return CType(innerlist.Item(index), Book)
    End Get
    Set(ByVal Value As Book)
    innerlist.Item(index) = Value
    End Set
    End Property

    Sub add(ByVal value As Book)
    innerlist.Add(value)
    End Sub
    End Class


    I then create a form, add a dropdownlist
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim objBooks As books = New books()
    objBooks.add(New Book("Book #1"))
    objBooks.add(New Book("Book #2"))
    objBooks.add(New Book("Book #3"))
    objBooks.add(New Book("Book #4"))
    objBooks.add(New Book("Book #5"))
    DropDownList1.DataSource = objBooks
    DropDownList1.DataTextField = "BookName"
    DropDownList1.DataBind()
    End Sub

    And i get this error

    DataBinder.Eval: 'Website.Products+Book' does not contain a property with
    the name BookName.
    When i access the object manually it works
    textbox1.text = objBooks.Item(0).BookName: Does work
    textbox1.text = objBooks(0).BookName: Does work
     
    Shrage Smilowitz, Oct 30, 2003
    #1
    1. Advertisements

  2. Shrage Smilowitz

    L. L. Guest

    The error message tells you what it is looking for. You have to make
    BookName a property.

    L.L.
     
    L. L., Oct 30, 2003
    #2
    1. Advertisements

  3. :) Thanks a million! I have ben working on it a whole day, i didn't realize
    that, i tought a public variable is the same think as a property. Thanks
    Again!
     
    Shrage Smilowitz, Oct 31, 2003
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.