Click here to Skip to main content
16,020,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Getting error mention in subject on line e.Item.FindControl("lst1").Visible
Item Template is in the Repeater

FE Code:

<ItemTemplate>
	
	<tr>
		<td align="left"  runat="server" id="tdList">

                        <ul  runat="server" id="lst1"><li>Lawsuit Report™ (Sunrise Only)</li><li>One Lawsuit Alarm</li></ul>

                        <ul  runat="server" id="lst2"><li>Lawsuit Report™</li><li>Three Lawsuit Alarms</li><li>One Weekly Lawsuit Tracker™</li><li>Advanced Search</li></ul>

                        <ul  runat="server" id="lst3"><li>Lawsuit Report™</li><li>Five Lawsuit Alarms</li><li>Three Daily Lawsuit Trackers™</li><li>Advanced Search</li></ul>


			   <img  runat="server" id="imgInfoBox" src="/images/icons/inline/information.png" 
               alt="Information" style="cursor:pointer; vertical-align:top;" />
               
						
		</td>


protected void rptSubscriptions_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lblExpireDate = (Label)e.Item.FindControl("lblDateExpire");
        Label lblPaymentProfileCCEnding = (Label)e.Item.FindControl("lblPaymentProfileCCending");
        Label lblPaymenMethod = (Label)e.Item.FindControl("lblPaymenMethod");

            int SubscriptionTypeID = 0;

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                SubscriptionTypeID = Convert.ToInt32(ds.Tables[0].Rows[i]["SubscriptionTypeID"]);

                if (SubscriptionTypeID == 22)
               { e.Item.FindControl("lst1").Visible = true; e.Item.FindControl("imgInfoBox").Visible = true; }     
               else if (SubscriptionTypeID == 23)
               { e.Item.FindControl("lst2").Visible = true; e.Item.FindControl("imgInfoBox").Visible = true; }
               else if (SubscriptionTypeID == 24)
               { e.Item.FindControl("lst3").Visible = true; e.Item.FindControl("imgInfoBox").Visible = true; }


            }
Posted
Comments
RahulRana723 11-Sep-13 3:01am    
ds.tables[0],Rows.Count give you this error.because there is no row in Table
Afzal Shaikh 11-Sep-13 3:12am    
i can see there are 12 rows are there.
RahulRana723 11-Sep-13 3:15am    
Plz post your full code...where you bind data and where you set ds.Tables.
Afzal Shaikh 11-Sep-13 3:17am    
this is the method which is binding repeater

public void GetUserSubscription()
{

ma = new MyAccount();

try { ma.UserID = Convert.ToInt32(Session["UserID"]); }
catch { Response.Redirect("/Login.aspx"); }

ds = ma.GetUserSubscription();

if (ds.Tables[0].Rows.Count > 0)
{
rptSubscriptions.DataSource = ds.Tables[0];
rptSubscriptions.DataBind();
}

}
Afzal Shaikh 11-Sep-13 3:17am    
Code for repeater in asp.net

<asp:Repeater runat="server" ID="rptSubscriptions"
onitemcreated="rptSubscriptions_ItemCreated"
onitemdatabound="rptSubscriptions_ItemDataBound" >

<HeaderTemplate>

<table width="100%" class="standard" cellspacing="0" cellpadding="4">

<tr style="background-color:#E1E1E1;">

<th align="center">Subscription Type</th>
<th align="center">Date Started</th>
<th align="center">Date Expired</th>
<th align="center">Cost</th>
<th align="center">Payment Method</th>
<th align="center">Options</th>
</tr>

</HeaderTemplate>

<itemtemplate>

<tr>
<td align="left" runat="server" id="tdList">

<ul visible="false" runat="server" id="lst1"><li>Lawsuit Report™ (Sunrise Only)</li><li>One Lawsuit Alarm</li></ul>

<ul visible="false" runat="server" id="lst2"><li>Lawsuit Report™</li><li>Three Lawsuit Alarms</li><li>One Weekly Lawsuit Tracker™</li><li>Advanced Search</li></ul>

<ul visible="false" runat="server" id="lst3"><li>Lawsuit Report™</li><li>Five Lawsuit Alarms</li><li>Three Daily Lawsuit Trackers™</li><li>Advanced Search</li></ul>


<img runat="server" id="imgInfoBox" src="/images/icons/inline/information.png"
alt="Information" style="cursor:pointer; vertical-align:top;" />


</td>

<td align="center" valign="top"><%# Eval("dateStarted")%></td>

<td align="center" valign="top">
<asp:Label runat="server" id="lblDateExpire">
</td>

<td align="center" valign="top"><%# Eval("AuthSubscriptionAmount")%></td>

<td align="center" valign="top">
<asp:Label runat="server" id="lblPaymentProfileCCending">
</td>

<td align="right" valign="top" id="tdPaymenMethod" >

<asp:Label runat="server" id="lblPaymenMethod" >


Change Payment Method
Cancel
</td>
</tr>



<footertemplate>
</table>

Object Reference errors are normally caused when accessing a member of an object that doesn't exist, e.g.
C#
Object o = null;
Console.WriteLine(o.ToString());


The problem usually occurs because of assumption - which is every programmer's biggest failing. In your case, there are many assumptions in one single function:

1.
C#
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

What happens if ds == null?
What happens if ds.Tables == null?
What happens if ds.Tables.Length == 0?

2.
C#
e.Item.FindControl("lst1").Visible
e.Item.FindControl("lst2").Visible
e.Item.FindControl("lst3").Visible
e.Item.FindControl("imgInfoBox").Visible

What happens if lst1 is not found (e.Item.FindControl returns null)?
What happens if lst2 is not found?
What happens if lst3 is not found?
What happens if imgInfoBox is not found?

Before using object members, you need to check the existence of the object first.

The worst of this is that they may never be null on your development kit - but as soon as it goes on someone else's machine (especially a customer!) this will fail. In fact, a customer will probably be able to make e.Item == null as well...
 
Share this answer
 
Comments
Afzal Shaikh 12-Sep-13 8:40am    
Thanks for giving tips for good programming practice :)
Make sure to check your list item type before doing the binding:

C#
if (!(e.Item.ItemType == ListItemType.AlternatingItem 
|| e.Item.ItemType == ListItemType.Item))
            return;


If it is neither of the two your ItemTemplate is either the Header or Footer, and that won't contain the control you are trying to find.
 
Share this answer
 
v2
Comments
Afzal Shaikh 11-Sep-13 2:29am    
Malhebe , I checked the type and Its returning false?
Eon Malherbe 11-Sep-13 2:34am    
If it returns false then you are currently databinding to the Header or Footer, and won't contain the control you are trying to find. The databinding event is called for all the repeater types including header and footer.
Afzal Shaikh 11-Sep-13 3:16am    
thanks Malherbe ,

Now repeater contain the controls and its returning true.Now can u plz tell me how to change value row by row for each control?

When i use this it made same value for all rows
e.item.FindControl("lst1").Visible

and when i use this it is throwing error of out of index (actually this is looking for item index but i need to work for item row by row)
rptSubscriptions.Items[i].FindControl("lst1").Visible
Eon Malherbe 11-Sep-13 3:22am    
e.Item.DataItem in the call will contain the DataRow object relevant to that row. You don't need to run an index over the data table or repeater.
Used gridview control instead of repeater to update value row by row for every control inside template field
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900