I am trying to loop through my repeater control and get the textbox values.
However, I am getting an error:
{"Object reference not set to an instance of an object."}
my code is:
Dim txtField As TextBox
Dim j As Integer = 0
'Confirm if user has entered atleast one quantity
For Each item In rptRequestForm.Items
txtField = rptRequestForm.FindControl("txtBox")
If txtField.Text <> Nothing Then
j += 1
Else
End If
Next
UPDATE: aspx code is:
<td><asp:Repeater ID="rptRequestForm" runat="server">
<HeaderTemplate>
<table border="0" width="100%">
<tr>
<td style="width:50%" class="TextFontBold"><asp:Label runat="server" ID="Label1" Text="Product"></asp:Label></td>
<td style="width:25%" class="TextFontBold"><asp:Label runat="server" ID="Label2" Text="Quantity"></asp:Label></td>
<td style="width:25%" class="TextFontBold"><asp:Label runat="server" ID="Label3" Text="Price (ea.)"></asp:Label></td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table border="0" width="100%">
<tr>
<td style="width:50%" class="TextFont"><span><%#Trim(Eval("Product_Title"))%></span></td>
<td style="width:25%"><asp:TextBox ID="txtBox" runat="server" Width="30%" onblur="Javascript:numberonly(this)"></asp:TextBox></td>
<td style="width:25%" class="TextFont"><span><%#Trim(FormatCurrency(Eval("Price")))%></span></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
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're trying to access the ".Text" property of a TextBox that it didn't find. The TextBox itself is the object to which there is no reference.
Incidentally, the .Text property of an actual Textbox (one that exists and was found) can't be "Nothing". It can only be String.Empty or a valid string.
Edited my line of code
Sorry, my VB is rusty.
Final edit
AARGH! I'm blind. I can't believe I didn't see this. There were TWO problems withthe original code. This is the answer to the second issue:
Change
txtField = rptRequestForm.FindControl("txtBox")
to
txtField = item.FindControl("txtBox")
The ITEM has to find the control, not the repeater itself!
I created a small web app just to check to see if I was grabbing the textbox's text and finally found the issue above. my code is NOT the same as yours in the aspx, but here's a complete code listing so that 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>
produces the following output in the System.Diagnostics.Debug window:
Item
Frank
AlternatingItem
Dave
Item
Muhammad
The thread 0x321c has exited with code 0 (0x0).
The thread 0x39b8 has exited with code 0 (0x0).