Search code examples
asp.netvb.netoledbrepeater

Trying to access only one textbox field inside a repeater template


  If (dr.Read) Then
            Dim thread As Integer
            thread = Convert.ToInt32(dr(0).ToString())
            For Each rItem As RepeaterItem In Repeater1.Items
                Dim txtBox As HtmlInputText = DirectCast(rItem.FindControl("messtxt"), HtmlInputText)
                If Not IsNothing(txtBox) Then
                    Dim messtxta As String = txtBox.Value
                    cmd2 = New OleDbCommand("INSERT INTO messages(content) VALUES('" & messtxta.Replace("'", "''") & "')", con)
                    cmd2.Connection = con
                    cmd2.ExecuteNonQuery()
                End If
            Next
            Response.Redirect("pals.aspx")

The above code works finely for inserting purposes. Except: The problem is, if the repeater repeats x number of times, there will be x number of inserts from the textbox, out of which one will be the one I entered.

I only want it to insert one time. Please help.


Solution

  • If (dr.Read) Then
            Dim thread As Integer
            Dim txtBox As HtmlInputText
            Dim messtxta As String
            thread = Convert.ToInt32(dr(0).ToString())
            For Each rItem As RepeaterItem In Repeater1.Items
                txtBox = DirectCast(rItem.FindControl("messtxt"), HtmlInputText)
             If Not IsNothing(txtBox) Then 
                IF txtBox.Value.Length > 0 THEN
                   messtxta   = txtBox.Value
                END IF
             End If   
            Next
    
            cmd2 = New OleDbCommand("INSERT INTO messages(content) VALUES('" & messtxta.Replace("'", "''") & "')", con)
            cmd2.Connection = con
            cmd2.ExecuteNonQuery()
    
            Response.Redirect("pals.aspx")