user1902540
user1902540

Reputation: 111

Pull Values From Textboxes and Concat into String w/ delimiter

I am trying to take the responses from 5 texts box values and then put them into a string, and eventually store them in a database but i cannot get the program to build the string, this is the code I have so far to build the string.

VB.Code

        Dim QuestionANS As String
        ' Get values from the text boxes
        Dim PassA As TextBox = CType(form1.FindControl("A"), TextBox)
        Dim PassB As TextBox = CType(form1.FindControl("B"), TextBox)
        Dim PassC As TextBox = CType(form1.FindControl("C"), TextBox)
        Dim PassD As TextBox = CType(form1.FindControl("B"), TextBox)
        Dim PassE As TextBox = CType(form1.FindControl("E"), TextBox)
        Dim PassF As TextBox = CType(form1.FindControl("F"), TextBox)

    QuestionANS = PassA.Text + "|" + PassB.Text + "|" + PassC.Text + "|" + PassD.Text + "|" + PassE.Text + "|" + PassF.Text

ASP Code

  <asp:DataList runat="server" ID="Questions">   
            <ItemTemplate>
               <table cellpadding="2px" cellspacing="0" border="0" style="page-break- inside:avoid;">
                <tr>
                   <td valign="top" style="padding-right:18px;"><asp:Label               runat="server" ID="QuestionNumber" /></td>

                    <td><asp:Image runat="server" style = "padding: 15px 0px 25px 0px;"  ID="Image" /></td>

                </tr>

                <tr>
                    <td></td>
            <td style="padding-top:10px;padding-bottom:15px;"></td>

                    <td style="padding-top:10px;padding-bottom:15px;"></td>
                    <tr>

                        <td style="width:50px;"><a>A:</a></td><td><asp:TextBox id="A" columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>B:</a></td> <td><asp:TextBox id="B"  columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>C:</a></td><td><asp:TextBox id="C" columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>D:</a></td><td><asp:TextBox id="D" columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>E:</a></td><td><asp:TextBox id="E" columns="30" runat="server" /></td>
                    </tr>
                    <tr>
                        <td style="width:50px;"><a>F:</a></td><td><asp:TextBox id="F" columns="30" runat="server" /></td>
                    </tr>
                </tr>

                    </table>
            <asp:HiddenField runat="server" ID="QuestionID" />
        </ItemTemplate>
    </asp:DataList>

Upvotes: 0

Views: 518

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460128

I assume that the TexBoxesare in a different NamingContainer like in a FormView or GridView. Then this would explain why you get a NullRefernceException on QuestionANS = PassA.Text.

Edit: Since you have now showed that it's in a ItemTemplate of a DataList:

For Each item As DataListItem In Questions.Items
    Dim PassA As TextBox = DirectCast(item.FindControl("A"), TextBox)
Next

or in the ItemDataBound event:

Protected Sub Item_Bound(sender As Object, e As DataListItemEventArgs) Handles Questions.ItemDataBound
    Select Case e.Item.ItemType
        Case ListItemType.Item, ListItemType.AlternatingItem
            Dim PassA As TextBox = DirectCast(e.Item.FindControl("A"), TextBox)
    End Select
End Sub

Upvotes: 2

Related Questions