Search code examples
sqlsql-servervb.nettextbox

Insert data from auto generated textbox to SQL Server database


I have created a code for generating a text box in vb.net using button click and function

 Public Function AddNewTextBox() As System.Windows.Forms.TextBox
    Dim txt As New System.Windows.Forms.TextBox()
    Me.Controls.Add(txt)
    txt.Top = cLeft * 30
    txt.Left = 100
    'txt.Text = "TextBox " & Me.cLeft.ToString
    cLeft = cLeft + 1
    txt.ForeColor = Color.DarkGreen
    txt.BackColor = Color.Gray
    txt.Font = New Font("Arial", 14.0, FontStyle.Regular)
    txt.Size = New Size(237, 31)
    txt.Location = New Point(156, 130 + top1)

    Return txt
End Function

In the button

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'call the function
        AddNewTextBox()

End Sub

I have tried this

cmd.CommandText = "INSERT INTO userlog ([username],[userlastname]) Values ( @username) "
cmd.Parameters.AddWithValue("@username", txt.Text(i).Text)
cmd.Parameters.AddWithValue("@userlastname", txt.Text(i).Text)

but getting an error in

txt.Text(i)

since txt is declared only in AddNewTextBox function.

I have made 3 auto generated text boxs

How do I save this data inside the text box to the database?


Solution

  • Add a FlowlayoutPanel to your form and set the FlowDirection to TopDown. (as commented by @jmcilhinney) This saves calculating the position of the text boxes.

    It doesn't make sense to have a function returning a text box when you never use the return value.

    The data access code uses the .Add method suggested by @SMor. See http://www.dbdelta.com/addwithvalue-is-evil/ and https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ and another one: https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications

    I had to guess at the datatypes. Check your database for correct types.

    The values come from the controls collection of the FlowLayoutPanel where the controls were added.

    Using blocks ensure that you database objects are closed and disposed even if there is an error. Pass the connection string directly to the constructor of the connection and the command text and connection directly to the constructor of the command.

    Public Sub AddNewTextBox()
        Dim txt As New System.Windows.Forms.TextBox()
        txt.Name = "user" & nameTextBox.ToString
        txt.ForeColor = Color.DarkGreen
        txt.BackColor = Color.Gray
        txt.Font = New Font("Arial", 14.0, FontStyle.Regular)
        txt.Size = New Size(120, 30)
        FlowLayoutPanel1.Controls.Add(txt)
    End Sub
    
    Private Sub UpdateUsers()
        Using cn As New SqlConnection("Your connection string")
            Using cmd As New SqlCommand("INSERT INTO userlog ([username],[userlastname]) Values ( @username, @userlastname);", cn)
                cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = FlowLayoutPanel1.Controls(0).Text
                cmd.Parameters.AddWithValue("@userlastname", SqlDbType.VarChar).Value = FlowLayoutPanel1.Controls(1).Text
                cn.Open()
                cmd.ExecuteNonQuery()
            End Using
        End Using
    End Sub
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        AddNewTextBox()
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        UpdateUsers()
    End Sub
    

    EDIT

        For Each tb As TextBox In FlowLayoutPanel1.Controls
            If tb.Text = "" Then
                MessageBox.Show("Please fill all text boxes before Updating")
                Return
            End If
        Next