470,620 Members | 1,460 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,620 developers. It's quick & easy.

looping through items in a repeater and fetching a dataitem field

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 '08 #1
4 5533
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
<ad*****@hotmail.comwrote in message
news:2d**********************************@l1g2000h sa.googlegroups.com...
>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 '08 #2
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 '08 #3
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
<ad*****@hotmail.comwrote in message
news:c8**********************************@q39g2000 hsf.googlegroups.com...
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 '08 #4
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 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Martin Dew | last post: by
3 posts views Thread by Julien Sobrier | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.