Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have the Dynamically created text boxes values.

VB
Public Class Form1
 
    WithEvents oBtnAdd As Button
    WithEvents oBtnSubst As Button
 

    Private Sub CreateDynamicControls()
        Dim but As Button = Nothing
        Dim txt As TextBox = Nothing
 
        Dim i As Integer
 
        For i = 1 To 10
            but = New Button
            With but
                .Location = New Point(500, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "ButtonSubst" & i
                .Width = 24
                .Text = "-"
                .Parent = Me
                AddHandler but.Click, AddressOf oBtnSubst_Click
            End With
 
            txt = New TextBox
            With txt
                .Location = New Point(525, 150 + (i * 25))
                .Name = "TextBox" & i
                .Tag = i.ToString
                .Width = 45
                .Text = 0
                .Enabled = False
                .Parent = Me
            End With
 
            but = New Button
            With but
                .Location = New Point(571, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "ButtonAdd" & i
                .Width = 24
                .Text = "+"
                .Parent = Me
                AddHandler but.Click, AddressOf oBtnAdd_Click
            End With
 

        Next i
    End Sub
 
    Public Sub oBtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles oBtnAdd.Click
        Dim btn As Button = sender
        Dim txt As TextBox = CType(Me.Controls("TextBox" & btn.Tag), TextBox)
        Dim count As Integer = Integer.Parse(txt.Text)
        txt.Text = count + 1
    End Sub
 
    Public Sub oBtnSubst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles oBtnSubst.Click
        Dim btn As Button = sender
        Dim txt As TextBox = CType(Me.Controls("TextBox" & btn.Tag), TextBox)
        Dim count As Integer = Integer.Parse(txt.Text)
        txt.Text = count - 1
    End Sub
 
    Public Sub New()
 
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        CreateDynamicControls()
    End Sub
End Class


I Need the above textbox values to be inserted to sql.

i tried below but only one data is getting saved. please help
VB
Dim con As New OleDb.OleDbConnection()
        Dim cmd As New OleDb.OleDbCommand()
        Dim i As Integer
        Dim selected As Object
        Dim it As Integer
        it = ListBox1.Items.Count
        Dim itemsCount As Integer = ListBox1.Items.Count
        For i = 0 To itemsCount - 1
            selected = ListBox1.Items.Item(i)
            con.ConnectionString = "Provider=SQLOLEDB.1;Password=$impadmins123;Persist Security Info=True;User ID=simp;Initial Catalog=Employee Information;Data Source=SRAVI6"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "insert into Agname (AgentName,t1,t2,t3,t4) values('" & Trim(selected) & "','" & Trim(txt.Text) & "','" & Trim(suptxt.Text) & "','" & Trim(abtxt.Text) & "','" & Trim(intxt.Text) & "')"
            cmd.ExecuteNonQuery()
            con.Close()
        Next i


Please help me saving the dynamically created textbox( all textbox values including the label ) to be inserted in sql table.
Posted
Comments
[no name] 16-Apr-13 23:31pm    
Hi,

why you are using for loop to access oledb connection, can you please give it in more detail, how many record you need to insert at one event.
Member 9989624 17-Apr-13 0:06am    
i need to insert all textboxs values. in short number of text boxes created dynamically.the number of text box depends on the number of items in listbox
Sergey Alexandrovich Kryukov 16-Apr-13 23:45pm    
Did you ever heard of separation of concerns?
How a TextBox issues can be related to SQL. For SQL, those values are just strings. For TextBoxes and other UI, SQL does not exist. Take one problem at a time, don't couple them.
—SA
Member 9989624 17-Apr-13 5:41am    
I dont have any concerns with txt boxes. Moreover there is difference in Separation and understanding. My query was clear and i have not coupled them. Anyways thanks for your time.

1 solution

Try this:

VB
selected = listBox1.Items(i).ToString
 
Share this answer
 
Comments
Member 9989624 17-Apr-13 5:41am    
this is getting the listbox items. I need the textbox values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900