to try
Dim someString as String = "Not set" <-- used later to hold the values of the string Dim txtField As TextBox Dim j As Integer = 0 'Confirm if user has entered atleast one quantity For Each item In rptRequestForm.Items txtField = item.FindControl("txtBox") If Not IsNothing(txtField) Then ' <--- this is the line I changed j += 1 someString = txtField.Text ' <-- once you've checked and know that the textbox exists, you just grab the value like so. ' do whatever you like with the contents of someString now. Else End If Next
The problem is that you are trying to access the ".Text" property of a text field that it did not find. The TextBox itself is an object for which there is no link.
By the way, the .Text property of a real text field (which exists and was found) cannot be "Nothing". It can be only String.Empty or a valid string.
Edited my line of code
Sorry, my VB is rusty.
Final editing
Aargh! I am blind. I can’t believe that I didn’t see this. There were two problems with the source code. This is the answer to the second question:
Edit
txtField = rptRequestForm.FindControl("txtBox")
to
txtField = item.FindControl("txtBox")
ITEM must find the control, not the repeater itself!
I created a small web application to check if I was grabbing the text of a text field and finally found the problem above. my code is NOT the same as yours in aspx, but here is a complete list of codes so you can see how it works:
vb code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim t As New System.Data.DataTable t.Columns.Add("Name") Dim newRow(1) As Object t.Rows.Add(New Object() {"Frank"}) t.Rows.Add(New Object() {"Dave"}) t.Rows.Add(New Object() {"Muhammad"}) rptRequestForm.DataSource = t rptRequestForm.DataBind() Dim txtField As TextBox Dim j As Integer = 0 'Confirm if user has entered atleast one quantity For Each item As RepeaterItem In rptRequestForm.Items txtField = item.FindControl("txtBox") If Not IsNothing(txtField) Then ' <--- this is the line I changed j += 1 System.Diagnostics.Debug.WriteLine(item.ItemType.ToString()) System.Diagnostics.Debug.WriteLine(txtField.Text) Else System.Diagnostics.Debug.WriteLine(item.ItemType.ToString()) End If Next End Sub
aspx code
<asp:Repeater ID="rptRequestForm" runat="server"> <HeaderTemplate> Hello! </HeaderTemplate> <ItemTemplate> <asp:TextBox ID="txtBox" runat="server" Text='<%#Bind("Name") %>'></asp:TextBox> <br /> </ItemTemplate> </asp:Repeater>
displays the following result in the System.Diagnostics.Debug window:
Paragraph
Franc
AlternatingItem
Dave
Paragraph
Muhammad
Thread 0x321c exited with code 0 (0x0).
Thread 0x39b8 exited with code 0 (0x0).
David
source share