你好,我有一个在vb中创建动态文本框和datetimepicker的代码,然后我想将其保存在数据库中,但是我的问题是我的代码比动态创建的项目保存的数据更多。我知道发生这种情况是因为我的代码在2循环内,但是我不知道如何从2循环中将其删除。请帮我谢谢。
我的代码在这里:
For Each textbox As TextBox In panelGroupDependent.Controls.OfType(Of TextBox)()
For Each dtp In panelGroupDependent.Controls.OfType(Of DateTimePicker)()
SaveRecord("INSERT INTO `tbl name`(`value1`, `value2`, `value3`) VALUES ('" & txt1.Text & "','" & textbox.Text & "','" & dtp.Value.ToString("yyyy-MM-dd") & "')")
Next
Next
MsgBox("Dependents Records Added Successfully", , "Dependents Records")
答案 0 :(得分:0)
您可以按照以下约定创建文本框和日期时间选择器:
Textbox1
,Textbox2
,Textbox3
... DateTimePicker1
,DateTimePicker2
,DateTimePicker3
... 然后,您可以使用如下代码:
for i = 1 to Me.panelGroupDependent.Controls.OfType(Of TextBox)().Count()
Dim txtBox As TextBox = CType(Me.Controls("Textbox" & i),TextBox)
Dim dtPicker As DateTimePicker = CType(Me.Controls("DateTimePicker"&i),DateTimePicker)
SaveRecord("INSERT INTO `tablename`(`value1`, `textbox`, `datetimepicker`) VALUES ('" & txtEmpNumber.Text & "','" & txtBox.Text & "','" & dtPicker.Value.ToString("yyyy-MM-dd") & "')")
Next