title: Find Label Control in Repeater Asp.net
body: <p>I am using repeater and I want to find the label control in my repeater. here is my code</p>
  
  <pre><code> &lt;asp:Repeater ID="friendRepeater" runat="server"&gt;
  
      &lt;table cellpadding="0" cellspacing="0"&gt;
  
      &lt;ItemTemplate&gt;
      &lt;tr style=" width:700px; height:120px;"&gt;
         &lt;td&gt;
         &lt;div style=" padding-left:180px;"&gt; 
             &lt;div id="leftHandPost" style="float:left; width:120px; height:120px; border: medium solid #cdaf95; padding-top:5px;"&gt;
                &lt;div id="childLeft" style=" padding-left:5px;"&gt;
                   &lt;div id="photo"  style=" border: thin solid black; width:100px;height:100px;"&gt;
                     &lt;asp:Image id="photoImage" runat="server" ImageUrl='&lt;%# String.Concat("Images/", Eval("Picture")) %&gt;' Width="100px" Height="100px" /&gt;
                   &lt;/div&gt;
                 &lt;/div&gt;&lt;!--childLeft--&gt;
              &lt;/div&gt;&lt;!--leftHandPost--&gt;
              &lt;/div&gt;
          &lt;/td&gt;
  
                          &lt;td&gt;
                              &lt;div id="rightHandPost" style=" float:right; padding-right:260px;"&gt;
                                  &lt;div id="childRight" style="width:400px; height:120px; border: medium solid #cdaf95; padding-top:5px; padding-left:10px;"&gt;
                                      &lt;strong&gt;&lt;asp:Label id="lblName" runat="server"&gt;&lt;%# Eval("PersonName") %&gt;&lt;/asp:Label&gt;&lt;/strong&gt;&lt;br /&gt;
                                      &lt;div style=" float:right; padding-right:10px;"&gt;&lt;asp:Button runat="server" Text="Add" onClick="add" /&gt;&lt;/div&gt;&lt;br /&gt;
                                      &lt;asp:Label id="lblID" runat="server"&gt;&lt;%# Eval("PersonID") %&gt;&lt;/asp:Label&gt;&lt;br /&gt;
                                      &lt;asp:Label id="lblEmail" runat="server"&gt;&lt;%# Eval("Email") %&gt;&lt;/asp:Label&gt;
                                  &lt;/div&gt;&lt;!--childRight--&gt;
                              &lt;/div&gt;&lt;!--rightHandPost--&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
  
      &lt;/ItemTemplate&gt;
  
      &lt;AlternatingItemTemplate&gt;
       &lt;tr style=" width:700px; height:120px;"&gt;
         &lt;td&gt;
         &lt;div style=" padding-left:180px;"&gt; 
             &lt;div id="Div1" style="float:left; width:120px; height:120px; border: medium solid #cdaf95; padding-top:5px;"&gt;
                &lt;div id="Div2" style="padding-left:5px;"&gt;
                   &lt;div id="Div3"  style=" border: thin solid black; width:100px;height:100px;"&gt;
                     &lt;asp:Image id="photoImage" runat="server" ImageUrl='&lt;%# String.Concat("Images/", Eval("Picture")) %&gt;' Width="100px" Height="100px" /&gt;
                   &lt;/div&gt;
                 &lt;/div&gt;&lt;!--childLeft--&gt;
              &lt;/div&gt;&lt;!--leftHandPost--&gt;
          &lt;/div&gt;
          &lt;/td&gt;
  
                          &lt;td&gt;
                              &lt;div id="Div4" style=" float:right; padding-right:260px;"&gt;
                                  &lt;div id="Div5" style="width:400px; height:120px; border: medium solid #cdaf95; padding-top:5px; padding-left:10px;"&gt;
                                      &lt;strong&gt;&lt;asp:Label id="lblName" runat="server"&gt;&lt;%# Eval("PersonName")%&gt;&lt;/asp:Label&gt;&lt;/strong&gt;
                                      &lt;div style=" float:right; padding-right:10px;"&gt;&lt;asp:Button id="btnAdd" runat="server" Text="Add" onClick="add"&gt;&lt;/asp:Button&gt;&lt;/div&gt;&lt;br /&gt;
                                      &lt;br /&gt;
                                      &lt;asp:Label id="lblID" runat="server"&gt;&lt;%# Eval("PersonID") %&gt;&lt;/asp:Label&gt;&lt;br /&gt;
                                      &lt;asp:Label id="lblEmail" runat="server"&gt;&lt;%# Eval("Email") %&gt;&lt;/asp:Label&gt;
                                  &lt;/div&gt;&lt;!--childRight--&gt;
                              &lt;/div&gt;&lt;!--rightHandPost--&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
      &lt;/AlternatingItemTemplate&gt;
  
      &lt;FooterTemplate&gt;
      &lt;/table&gt;
      &lt;/FooterTemplate&gt;
  
  &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>Here is the code behind for the add button. </p>
  
  <pre><code>protected void add(object sender, EventArgs e)
      {           
          DateTime date = DateTime.Now;
          System.Web.UI.WebControls.Label la = (System.Web.UI.WebControls.Label)friendRepeater.FindControl("PersonID");
          String id = la.Text;
  
          try
          {
             MySqlConnection connStr = new MySqlConnection();
             connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
             String insertFriend = "INSERT INTO contactFriend(friendID, PersonID, PersonIDB, date) values (@id, @personIDA, @personIDB, @date)";
             MySqlCommand cmdInsertStaff = new MySqlCommand(insertFriend, connStr);
             cmdInsertStaff.Parameters.AddWithValue("@id", "F000004");
             cmdInsertStaff.Parameters.AddWithValue("@personIDA", "M000001");
             cmdInsertStaff.Parameters.AddWithValue("@personIDB", id);
             cmdInsertStaff.Parameters.AddWithValue("@date", date);
             connStr.Open();
             cmdInsertStaff.ExecuteNonQuery();
  
             MessageBox.Show("inserted");
             connStr.Close();
  
           }
           catch (Exception ex)
           {
              MessageBox.Show(ex.ToString());
           }            
      }
  </code></pre>
  
  <p>I have get the error of Object reference not set to an instance of an object. I think is because there are no value in the Label. The Find Control are not working. May I know how can fix this problem? Thank you very much</p>