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:
for i As Integer = 0 To cntPos -1
Dim txtJobTo as TextBox = new TextBox()
txtJobTo.ID = "txtJobTo_" & jobID
Dim label as Label = new Label()
label.text = posPanel.Rows(i).Item("Position")
pnlContainer.Controls.Add(label)
pnlContainer.Controls.Add(new LiteralControl("</td><td>"))
pnlContainer.Controls.Add(txtJobTo)
next
This line of code is shows those texboxes on page
<tr bgcolor="#FFCC99"><td colspan="4">
<asp:Panel ID="pnlContainer" runat="server" Visible="true"></asp:Panel></td></tr>
<tr><td colspan='6' align='center'> <asp:Button ID="cmdSave" Text="Save" ToolTip="cmdSave" CommandArgument="cmdSave_Click" runat="server" /></td></tr>
And this is part of the code that should save all data
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_" & posID.ToString()),TextBox).Text
....
'Insert into database
next
I always get error message on this line strTo.Text =CType(pnlContainer.FindControl("txtJobTo_" & posID.ToString()),TextBox).Text 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