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
            SqlQuery(sql)
            a = a + 1
        End While

    End Sub

any ideas?

Recommended Answers

All 4 Replies

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.

Either:
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 & "');"

Or:
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);

can you give me an example of your 2nd statement the parameters i think that is much easier

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.