-2

So I have a three forms with text boxes, and I'm saving them to db on the last one, one is pretty huge and iterative so I'd like to write a loop. But when I try to loop through them it doesn't seem to grab them from the other form, what am I doing wrong?

 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

As you see I tried different combinations. The error I'm getting is:

"System.NullReferenceException: 'Object reference not set to an instance of an object.' txtbox_bem was Nothing."

Sunil
  • 3,404
  • 10
  • 23
  • 31
TozlaBe
  • 28
  • 2
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Blackwood Mar 26 '18 at 01:34
  • I know what null is, this question is about a specific issue. How do you grab a textbox that is on a different so I can loop through it – TozlaBe Mar 26 '18 at 01:39
  • 1
    So, you have 96 input controls on one form and the data goes into 12 different tables? Hmm. How about databinding? If you want to write SQL statements you, really need to use Parameters and not concatenate strings. CheckBox.Text is not what you want. I think you want the Checked property which will get you a boolean. – Mary Mar 26 '18 at 04:30
  • why you initializing controls inside the loop? – PNP Mar 26 '18 at 04:40
  • 1
    @pnp because the variables only point towards the existing textboxes and they only exist to transfer data into the db? – David Wilson Mar 26 '18 at 09:57
  • @mary Didn't notice the checkbox. Also didn't design the table either, but its a workable solution. As for paramaters, will prolly add them later along with validation – TozlaBe Mar 26 '18 at 13:29
  • @PNP So I can use variable i in their name, and reference a different each time, so I can loop through them easily and grab their content – TozlaBe Mar 26 '18 at 13:29
  • @David Wilson - i think that you have to answer the question of TozlaBe. – PNP Mar 26 '18 at 16:30

2 Answers2

1

The line ..

Dim txtbox_bem As TextBox = CType(frm_new_prot_2.Controls("txtbox_bem1_.txtbox_bem1_" & i), TextBox)

is searching through the controls of frm_new_prot_2 for a control called "frm_new_prot_2.txtbox_bem1_" & i.

That won't exist. In the controls collection will only contain the textbox with a name of "txtbox_bem1_" & i, so the corrected version should be

Dim txtbox_bem As TextBox = CType(frm_new_prot_2.Controls("txtbox_bem1_" & i), TextBox)

The other lines in your code probably won't work either. They should probably be ..

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)

I think you got a bit confused about how references work. :-)


Also I suspect that the data in the textboxes is the only place where you're storing the data. This isn't terribly good programming practice. For example...

Say, the data comes from an external device and you have code that reads data from the device and stores it in the textboxes. This isn't good. anyone could accidentally overwrite data in the textbox.

It would be much better to store the data in Lists or Lists of class objects. and populate the textboxes with the data. When it comes to storing the data in your db, use the data in the lists rather than the textboxes.

The user interface should never be used to store data. Hope this helps

David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • Wow, thanks a lot for copying and editing all 8 of them but unfortunately it didn't work. I already tried that combination, I tried all the combinations that I listed in the question, ofc made sure the combination was at the top so that it gets tested and not break before it gets to them. https://i.gyazo.com/ef6fa3d38b05cbdfd15c7b9819d9c342.png – TozlaBe Mar 26 '18 at 13:25
  • It may be the way that you instantiated the form. If you're using the defaul instance, it should work, I tried it on my own pc by creating a main form, another form with the same name as yours and a third form that tries to access the data from "your" form and it worked fine. If you created a new instance in your main form class, it may not work. – David Wilson Mar 26 '18 at 16:22
0

It might be easier to debug without all the casting.

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

I just added the List for testing purposes.

Mary
  • 14,926
  • 3
  • 18
  • 27