title: How to get value from dynamically created texboxes?
body: <p>im new here and im having some problem with my vb.net code. I have dynamically created few textboxes on panel, and I want my code after submitting the form to save those values into database. Problem is that I just cannot find the way how to grab values from those texboxes. I tried everything that i found on internet but i still getting the error message "Object reference not set to an instance of an object". Can someone advice me what am i doing wrong.
  Here is the code how i created texboxes:</p>
  
  <pre><code> for  i As Integer = 0 To cntPos -1
          Dim  txtJobTo as TextBox = new TextBox()
          txtJobTo.ID = "txtJobTo_" &amp; jobID
          Dim label as Label = new Label()
          label.text = posPanel.Rows(i).Item("Position")
  
          pnlContainer.Controls.Add(label)
          pnlContainer.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;td&gt;"))
          pnlContainer.Controls.Add(txtJobTo)
  next
  </code></pre>
  
  <p>This line of code is shows those texboxes on page</p>
  
  <pre><code>&lt;tr  bgcolor="#FFCC99"&gt;&lt;td colspan="4"&gt;
   &lt;asp:Panel ID="pnlContainer" runat="server" Visible="true"&gt;&lt;/asp:Panel&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td colspan='6' align='center'&gt; &lt;asp:Button ID="cmdSave" Text="Save" ToolTip="cmdSave" CommandArgument="cmdSave_Click" runat="server" /&gt;&lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>And this is part of the code that should save all data</p>
  
  <pre><code>Sub cmdSave_Click(ByVal sender As System.Object,  e As EventArgs) Handles cmdSave.Click  
     ...
  
          for  i As Integer = 0 To cntPosIns -1
              Dim strTo as TextBox = New TextBox()     
              posID = posIns.Rows(i).Item("ID_Pos")
             strTo.Text =CType(pnlContainer.FindControl("txtJobTo_" &amp; posID.ToString()),TextBox).Text
  ....
  'Insert into database
  next
  </code></pre>
  
  <p>I always get error message on this line <em>strTo.Text =CType(pnlContainer.FindControl("txtJobTo_" &amp; posID.ToString()),TextBox).Text</em>
  Can someone please give me some advice on how to fix this?  What is the proper way to read value from dynamically created textboxes in this situation?
  Thank you</p>