所以我有三个带有文本框的表单,我将它们保存到db的最后一个,一个非常庞大且迭代,所以我想写一个循环。但是当我试图循环它们时,它似乎并没有从其他形式中获取它们,我做错了什么?
For i = 1 To 12
Dim txtbox_bem As TextBox = CType(frm_new_prot_2.Controls("frm_new_prot_2.txtbox_bem1_" & i), TextBox)
Dim txtbox_soll As TextBox = CType(frm_new_prot_2.Controls("txtbox_soll1_" & i), TextBox)
Dim txtbox_messw As TextBox = CType(frm_new_prot_2.Controls("txtbox_messw1_" & i), TextBox)
Dim txtbox_einheit As TextBox = CType(Controls("frm_new_prot_2.txtbox_einheit1_" & i), TextBox)
Dim txtbox_max As TextBox = CType(Controls("frm_new_prot_2.txtbox_max1_" & i), TextBox)
Dim txtbox_min As TextBox = CType(Controls("frm_new_prot_2.txtbox_min1_" & i), TextBox)
Dim txtbox_tol As TextBox = CType(Controls("frm_new_prot_2.txtbox_tol1_" & i), TextBox)
Dim ckbox_ok As CheckBox = CType(Controls("frm_new_prot_2.ckbox_ok1_" & i), CheckBox)
command.CommandText = "INSERT INTO tb_1_" & i & " (Auftraggeber, Protokollnr, 1_" & i & "bem, 1_" & i & "soll, 1_" & i & "messw, 1_" & i & "einheit, 1_" & i & "max, 1_" & i & "min, 1_" & i & "tol, 1_" & i & "ok) VALUES ('" & Auftraggeber & "'," & Protokollnr & ",'" & txtbox_bem.Text & "','" & txtbox_soll.Text & "','" & txtbox_messw.Text & "','" & txtbox_einheit.Text & "','" & txtbox_max.Text & "','" & txtbox_min.Text & "','" & txtbox_tol.Text & "','" & ckbox_ok.Text & "')"
command.CommandType = CommandType.Text
command.ExecuteNonQuery()
MsgBox("success row " & i)
Next
如你所见,我尝试了不同的组合。我得到的错误是:
“System.NullReferenceException:'对象引用未设置为对象的实例。' txtbox_bem没什么。“
答案 0 :(得分:1)
该行......
Dim txtbox_bem As TextBox = CType(frm_new_prot_2.Controls("txtbox_bem1_.txtbox_bem1_" & i), TextBox)
正在frm_new_prot_2
的控件中搜索名为"frm_new_prot_2.txtbox_bem1_" & i
的控件。
那不存在。在控件集合中,只包含名称为"txtbox_bem1_" & i
的文本框,因此更正的版本应为
Dim txtbox_bem As TextBox = CType(frm_new_prot_2.Controls("txtbox_bem1_" & i), TextBox)
代码中的其他行可能也不会起作用。他们应该是......
Dim txtbox_bem As TextBox = CType(frm_new_prot_2.Controls("txtbox_bem1_" & i), TextBox)
Dim txtbox_soll As TextBox = CType(frm_new_prot_2.Controls("txtbox_soll1_" & i), TextBox)
Dim txtbox_messw As TextBox = CType(frm_new_prot_2.Controls("txtbox_messw1_" & i), TextBox)
Dim txtbox_einheit As TextBox = CType(frm_new_prot_2.Controls("txtbox_einheit1_" & i), TextBox)
Dim txtbox_max As TextBox = CType(frm_new_prot_2.Controls("txtbox_max1_" & i), TextBox)
Dim txtbox_min As TextBox = CType(frm_new_prot_2.Controls("txtbox_min1_" & i), TextBox)
Dim txtbox_tol As TextBox = CType(frm_new_prot_2.Controls("txtbox_tol1_" & i), TextBox)
Dim ckbox_ok As CheckBox = CType(frm_new_prot_2.Controls("ckbox_ok1_" & i), CheckBox)
我认为你对引用如何工作有点困惑。 : - )
此外,我怀疑textboxes
中的数据是您存储数据的唯一地方。这不是一个非常好的编程实践。例如......
比如说,数据来自外部设备,你有代码从设备读取数据并将其存储在textboxes
中。这不好。任何人都可能意外覆盖textbox
中的数据。
将数据存储在类对象的Lists
或Lists
中要好得多。并使用数据填充textboxes
。在数据库中存储数据时,请使用lists
而不是textboxes
中的数据。
永远不应该使用用户界面来存储数据。希望这有帮助
答案 1 :(得分:0)
如果不进行全部投射,调试可能会更容易。
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lst As New List(Of String)
For i As Integer = 1 To 3
Dim str1 As String = Form1.Controls("TextBox" & i).Text
lst.Add(str1)
Next
For Each s As String In lst
Debug.Print(s)
Next
End Sub
End Class
我刚刚添加了List用于测试目的。