Getting the data bound to a Repeater

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Dec 6, 2007.

  1. I am attempting to get the data that was bound to an item in a Repeater
    control for use in the ItemDataBound event. Here is the code I am using to
    attempt to retrieve the data:

    CType(e.Item.DataItem, DataRowView)("fieldname")

    This has normally worked for me in the past, but once in a while it gives me
    a problem for reasons I cannot understand. The error I am recieving is:

    Object reference not set to an instance of an object.

    I know that the datatable that I am binding to the Repeater has data in it,
    because the data is displayed on the page if I comment out this line of the
    code. Does anybody have any idea what I am doing wrong? Thanks.
     
    Nathan Sokalski, Dec 6, 2007
    #1
    1. Advertising

  2. It might not be the correct item type because of header templates etc., e.g.
    (C#):

    if( e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)
    {
    // FindControl code here
    }
    --
    Site: www.eggheadcafe.com
    UnBlog: petesbloggerama.blogspot.com
    Metafinder: www.blogmetafinder.com



    "Nathan Sokalski" wrote:

    > I am attempting to get the data that was bound to an item in a Repeater
    > control for use in the ItemDataBound event. Here is the code I am using to
    > attempt to retrieve the data:
    >
    > CType(e.Item.DataItem, DataRowView)("fieldname")
    >
    > This has normally worked for me in the past, but once in a while it gives me
    > a problem for reasons I cannot understand. The error I am recieving is:
    >
    > Object reference not set to an instance of an object.
    >
    > I know that the datatable that I am binding to the Repeater has data in it,
    > because the data is displayed on the page if I comment out this line of the
    > code. Does anybody have any idea what I am doing wrong? Thanks.
    >
    >
     
    Peter Bromberg [C# MVP], Dec 6, 2007
    #2
    1. Advertising

  3. Hi Nathan,


    Before you access assiociated DataItem check the current item's type:
    If e.Item.ItemType = ListItemType.Item or _
    e.Item.ItemType = ListItemType.Item then

    CType(e.Item.DataItem, DataRowView)("fieldname")

    end if

    hope it helps
    --
    Milosz


    "Nathan Sokalski" wrote:

    > I am attempting to get the data that was bound to an item in a Repeater
    > control for use in the ItemDataBound event. Here is the code I am using to
    > attempt to retrieve the data:
    >
    > CType(e.Item.DataItem, DataRowView)("fieldname")
    >
    > This has normally worked for me in the past, but once in a while it gives me
    > a problem for reasons I cannot understand. The error I am recieving is:
    >
    > Object reference not set to an instance of an object.
    >
    > I know that the datatable that I am binding to the Repeater has data in it,
    > because the data is displayed on the page if I comment out this line of the
    > code. Does anybody have any idea what I am doing wrong? Thanks.
    >
    >
     
    Milosz Skalecki [MCAD], Dec 6, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rhiner Dan
    Replies:
    1
    Views:
    773
    Mike Wahler
    Mar 27, 2005
  2. Mario Krsnic
    Replies:
    0
    Views:
    398
    Mario Krsnic
    Jun 23, 2006
  3. John Kotuby
    Replies:
    3
    Views:
    345
    John Kotuby
    Apr 19, 2007
  4. mark4asp
    Replies:
    1
    Views:
    597
    mark4asp
    Dec 3, 2007
  5. Nathan Sokalski

    Getting the data bound to a Repeater

    Nathan Sokalski, Dec 6, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    322
    Milosz Skalecki [MCAD]
    Dec 6, 2007
Loading...

Share This Page