0

In my application reading value from front end into label lbBillableAmount1 but somehow null value is passed to it and code block terminates and transfers to catch block.Please refer below code:

 protected void RPClientWish_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if(Convert.ToInt32(Session["RoleId"])==1)
            {
                Label lb2 = (Label)e.Item.FindControl("lb2");
                lb2.Visible = true;
                Label lbBillableAmount1 = (Label)e.Item.FindControl("Label7");

                lbBillableAmount1.Visible = true;
            }
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }

    }

Please let me know solution for this because even after dropping label and creating new one still getting error. Error is :Object reference not set to an instance of object. I have read lot of posts for this error but nothing is solving the issue.

Here is my repeater code:

    <asp:Repeater ID="RPClientWish" runat="server" OnItemDataBound="RPClientWish_ItemDataBound" >
       <HeaderTemplate>
        <div class="col-md-12">
     <div class="EmployeeList">
             <div class="col-md-3">
     <asp:Label ID="lbname" runat="server" Font-Bold="true" Text="Name" style="font-family:'Segoe UI';"></asp:Label>
           </div>
    <div class="col-md-1"> &nbsp;</div>
     <div class="col-md-1">&nbsp;</div>
    <div class="col-md-2"><asp:Label ID="Label2" runat="server" Font-Bold="true" Text="Hours" style="font-family:'Segoe UI';"></asp:Label></div>
      <div class="col-md-1">&nbsp;</div>
                                                                <div class="col-md-2"><asp:Label ID="lb1" runat="server" Font-Bold="true" Text="Billable Hours" style="font-family:'Segoe UI';"></asp:Label></div>
        <div class="col-md-2">
      <asp:Label ID="lb2" runat="server" Font-Bold="true" Text="Billable Amount" style="font-family:'Segoe UI';" Visible="false"></asp:Label>
                   </div>
         </div>
      </div>
           </HeaderTemplate>
         <ItemTemplate>
         <div class="col-md-12">
             <div class="EmployeeList">
            <div class="col-md-3">
<asp:Label ID="lbClientname" runat="server" Font-Bold="true" Text='<%# Eval("ClientName")%>' style="font-family:'Segoe UI';"></asp:Label>
                                                                </div>
            <div class="col-md-1"> &nbsp;</div>
     <div class="col-md-1">&nbsp;</div>
   <div class="col-md-2"><asp:Label ID="lbhours" runat="server" Font-Bold="true" Text='<%# Eval("TotalHours")%>' style="font-family:'Segoe UI';"></asp:Label></div>
 <div class="col-md-1">&nbsp;</div>
<div class="col-md-2"><asp:Label ID="lbBillableHours1" runat="server" Font-Bold="true" Text="00" style="font-family:'Segoe UI';" ></asp:Label></div>
      <div class="col-md-2">

**<asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
      </div>**
    </div>
    <div class="col-md-12"><hr /></div>
   </div>

 </ItemTemplate>

  </asp:Repeater>
user3714403
  • 101
  • 12

1 Answers1

0

As label7 is defined in ItemTemplate so you have to search that label only in ListItemType as Item. For first time when event got fired it has HeaderTemplate so it doesn't find any Lable with id lable7 that causes error.So here is the code to access lable7

    protected void RPClientWish_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                if(Convert.ToInt32(Session["RoleId"])==1)
                {
                 if(e.Item.ItemType == ListItemType.Header)
                   {
                    Label lb2 = (Label)e.Item.FindControl("lb2");
                    lb2.Visible = true;
                   }
                 else if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                   {
                    Label lbBillableAmount1 = (Label)e.Item.FindControl("Label7");

                    lbBillableAmount1.Visible = true;
                  }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
Rojalin Sahoo
  • 997
  • 1
  • 6
  • 18
  • Used above code block but now getting null exception at lb2 – user3714403 Oct 28 '15 at 09:37
  • `lb2` is in `HeaderTemplate` for that it is showing error. let me update my code. – Rojalin Sahoo Oct 28 '15 at 09:39
  • This code is working but in repeater control it is displaying data for first row only and not for other rows.While debugging it jumps to other code block instead of going in else if block. – user3714403 Oct 28 '15 at 10:06
  • first row means only for HeaderTemplate ? Is there any error? – Rojalin Sahoo Oct 28 '15 at 10:10
  • Not for header template,this is properly visible.It is displaying data in item template but only value for first row and not after that – user3714403 Oct 28 '15 at 10:20
  • also add `e.Item.ItemType == ListItemType.AlternatingItem` in else if section `else if(e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem )` – Rojalin Sahoo Oct 28 '15 at 10:27