Hi,
I have the Dynamically created text boxes values.
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()
InitializeComponent()
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
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.