looping through items in a repeater and fetching a dataitem field

Discussion in 'ASP .Net' started by adiel_g@hotmail.com, Jan 9, 2008.

  1. Guest

    I am trying to loop through a repeater to retrieve a dataitem field
    but am getting a NullReferenceException.
    I can find a checkbox control but cannot find a dataitem field. Here
    is the code that is looping through the repeater:

    Dim rc As RepeaterItemCollection = rptReport.Items
    Dim ri As RepeaterItem
    Dim chkSelected As System.Web.UI.WebControls.CheckBox
    Dim customer As String

    For Each ri In rc
    chkSelected = CType(ri.FindControl("chkCaseSel"),
    System.Web.UI.WebControls.CheckBox)
    If chkSelected.Checked Then
    customer = ri.DataItem("customer") ' this is
    the error line
    getCustomersSelected.Add(casenum) ' this just adds
    the customer to an array list
    End If
    Next

    As you can see the line failing is:

    customer = ri.DataItem("customer") ' this is the error line

    The error is "NullReferenceException was unhandled by user code"

    Thanks Before Hand,
    Adiel
    , Jan 9, 2008
    #1
    1. Advertising

  2. DataItem is available only in the ItemDataBound event. Makes perfect sense
    if you think of it since after the ItemDataBound items don't need anything
    from the datasource, all data are already inside the items.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    <> wrote in message
    news:...
    >I am trying to loop through a repeater to retrieve a dataitem field
    > but am getting a NullReferenceException.
    > I can find a checkbox control but cannot find a dataitem field. Here
    > is the code that is looping through the repeater:
    >
    > Dim rc As RepeaterItemCollection = rptReport.Items
    > Dim ri As RepeaterItem
    > Dim chkSelected As System.Web.UI.WebControls.CheckBox
    > Dim customer As String
    >
    > For Each ri In rc
    > chkSelected = CType(ri.FindControl("chkCaseSel"),
    > System.Web.UI.WebControls.CheckBox)
    > If chkSelected.Checked Then
    > customer = ri.DataItem("customer") ' this is
    > the error line
    > getCustomersSelected.Add(casenum) ' this just adds
    > the customer to an array list
    > End If
    > Next
    >
    > As you can see the line failing is:
    >
    > customer = ri.DataItem("customer") ' this is the error line
    >
    > The error is "NullReferenceException was unhandled by user code"
    >
    > Thanks Before Hand,
    > Adiel
    Eliyahu Goldin, Jan 9, 2008
    #2
    1. Advertising

  3. Guest

    Thanks, so how would I access the fields in the above loop without
    using the DataItem property?

    Thanks Again,
    Adiel

    >>DataItem is available only in the ItemDataBound event. Makes perfect sense
    >>if you think of it since after the ItemDataBound items don't need anything
    >>from the datasource, all data are already inside the items.
    , Jan 9, 2008
    #3
  4. If you mean the fields of the datasource, you can get them only in the
    ItemDataBound event that fires for every item separately.

    If you mean the fields in the repeater, you don't need DataItem for them.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


    <> wrote in message
    news:...
    > Thanks, so how would I access the fields in the above loop without
    > using the DataItem property?
    >
    > Thanks Again,
    > Adiel
    >
    > >>DataItem is available only in the ItemDataBound event. Makes perfect

    sense
    > >>if you think of it since after the ItemDataBound items don't need

    anything
    > >>from the datasource, all data are already inside the items.
    Eliyahu Goldin, Jan 9, 2008
    #4
  5. Guest

    I think I understand what you mean. The datasource is not stored as
    part of the repeater. So since it's not stored as part of the
    repeater, the fields from the datasource would only be available while
    the databinding is taking place (ItemDataBound). With this in mind
    the workaround would be to hold the data needed into a portion of the
    repeater that will remain after databinding. In another words holding
    the data in a control defined inside the repeater control. One
    example would be a label control. With this in mind, I set the data
    inside a label and set the label's property to invisible:

    <asp:label id="lblCustomer" runat="server" Visible="False" Text='<%#
    DataBinder.Eval(Container.DataItem, "Customer") %>'></asp:label>

    This solved my problem since now the field was available AFTER
    databinding (ItemDataBound).

    It is interesting how there is a dataitem property in the RepeaterItem
    object:


    Dim ri As RepeaterItem
    ....
    customer = ri.DataItem("customer") ' object is there but data is not
    there...


    Of course this object is only available during binding time
    (ItemDataBound) as described above by Eliyahu. So for now, a
    workaround such as above must be used. If someone at Microsoft is
    reading this, it would be nice for a future version of .NET if the
    datasource would be available after the ItemDataBound. It looks like
    they did not do this because of the overhead. But if overhead is the
    issue, you can even make it an option during binding to include the
    datasource in the repeater item or to not include it if you think its
    an overhead. For example:


    ' Bind to repeater
    rptReport.DataSource = dt
    rptReport.IncludeDataSourceAfterBinding = True ' THIS WOULD
    BE THE NEW PROPERTY: Would default to false as it works now...
    rptReport.DataBind()


    Thanks,
    Adiel
    , Jan 10, 2008
    #5
    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. MattB
    Replies:
    0
    Views:
    448
    MattB
    May 18, 2004
  2. Alan Silver
    Replies:
    2
    Views:
    11,049
    Alan Silver
    Jul 19, 2005
  3. Bart Van Hemelen
    Replies:
    2
    Views:
    4,666
    Bart Van Hemelen
    Sep 4, 2006
  4. WC Justice

    Build SQL string looping through field names

    WC Justice, Jan 24, 2005, in forum: ASP General
    Replies:
    2
    Views:
    182
    Bob Barrows [MVP]
    Jan 24, 2005
  5. Replies:
    0
    Views:
    119
Loading...

Share This Page