how do i insert the textboxes with different data inside of them in one database table

i have this code dunno what to do next

 Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim sql As String
        Dim totcount As Integer = 0
        Dim inc As Integer = 0
        Dim counttome As Integer = 4

        sql = "INSERT INTO table ('name') VALUES ('" & TextBox1.Text & "')"
        sql = "INSERT INTO table ('name') VALUES ('" & TextBox2.Text & "')"
        sql = "INSERT INTO table ('name') VALUES ('" & TextBox3.Text & "')"
        sql = "INSERT INTO table ('name') VALUES ('" & TextBox4.Text & "')"
        Dim a As Integer = 0
        ' while loop execution '
        While a < 5
            a = a + 1
        End While

    End Sub

any ideas?

Edited by joshuajames.delacruz

Attachments sample.png 4.76 KB
2 Years
Discussion Span
Last Post by joshuajames.delacruz

You could specify your SQL command with a parameter and then, in the loop, set the parameter to each text box. Or you can build up a long SQL string, separating the individual commands with semi-colons (;).
Your code is a bit jumbled as you set your sql variable to include textbox1, then textbox2, then textbox2 etc. At the end it is only containing the code to insert textbox4. And you then use a loop to insert textbox4 5 times.

sql = "INSERT INTO table ('name') VALUES ('" & TextBox1.Text & "');INSERT INTO table ('name') VALUES ('" & TextBox2.Text & "');INSERT INTO table ('name') VALUES ('" & TextBox3.Text & "');INSERT INTO table ('name') VALUES ('" & TextBox4.Text & "');"

sql = "INSERT INTO table ('name') VALUES (?textbox);"
This would involve including a command parameter into your code which you don't have yet. And in the loop setting the parameter to the value of each textbox.


im avoiding the either statement of yours because if one of those textboxes are empty it will still insert right?


Yes, it would. If you don't want to include empty values then either option would still work but you would add a acheck to see if a particular textbox was empty before appending it to the sql string (option 1) or running the command (option 2);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.