Insert only enabled textbox into MySQL in VB.Net
P粉043432210
P粉043432210 2024-02-21 19:42:03
0
1
343

I am creating an inventory management system in VB.Net whose basic functionality is to process incoming invoices. I want to insert data into my remote MySQL database, but only if the textbox is considered enabled. I have no problem inserting data into the database when I want all fields to be inserted. But I want the checkboxes to enable certain products and allow employees to enter only specific items. The checkboxes do enable and disable the text fields as needed, but when the data is inserted into the database it will enter null values ​​for all product types, which I don't want it to do as it will mess up the invoicing system. Currently I've tried an if then statement but the problem I'm having is that it expects a textbox to be defined which is not enabled. The code I've tried is:

Public Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click

    Dim mysqlconn as new MySqlConnection ("ServerConnection")
    mysqlconn.Open()
    dim mysqlCmd as new MysqlCommand
    mysqlcmd.Connection = MysqlConn
    
    mysqlcmd.CommandText = "Insert into Table_Name (Column1,Column2,Column3) Values (@rec1,@Rec2,@Rec3)"
    
    If txtTextbox1.Enabled = True then
        mysqlcmd.Parameters.AddWithValue("@Rec1",Column1.text)
    End If
    
    If txtTextBox2.Enabled = True then
        mysqlcmd.Parameters.AddWithValue(@Rec2,Column2.text)
    End IF
    
    IF txtTextBox3.Enabled = True then
        mysqlcmd.Parameters.AddWithValue(@Rec3,Column3.text)
    End If

P粉043432210
P粉043432210

reply all(1)
P粉739706089

You can't just insert a column without rows. So if you want to insert one column, you have to insert all columns. If you don't want to add a value, the column can be NULL or an empty string, depending on the column in the database. You can also use Using and use Async/Await to remove the database work from the UI

Public Async Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
    ' if your columns take NULL to mean no value
    Await AddParams(
        If(txtTextbox1.Enabled, Column1.Text, Nothing),
        If(txtTextbox2.Enabled, Column2.Text, Nothing),
        If(txtTextbox3.Enabled, Column3.Text, Nothing))
    ' else, if an empty string means no value
    Await AddParams(
        If(txtTextbox1.Enabled, Column1.Text, ""),
        If(txtTextbox2.Enabled, Column2.Text, ""),
        If(txtTextbox3.Enabled, Column3.Text, ""))
End Sub

Private Function AddParams(param1 As String, param2 As String, param3 As String) As Task
    Return Task.Run(
    Sub()
        Using mysqlconn As New MySqlConnection("ServerConnection")
            mysqlconn.Open()
            Using mysqlCmd As New MySqlCommand("Insert into Table_Name (Column1,Column2,Column3) Values (@rec1,@Rec2,@Rec3)", mysqlconn)
                mysqlCmd.Parameters.AddWithValue("@Rec1", param1)
                mysqlCmd.Parameters.AddWithValue("@Rec2", param2)
                mysqlCmd.Parameters.AddWithValue("@Rec3", param3)
            End Using
        End Using
    End Sub)
End Function

If I understand your current design,

Table name

ID Column 1 Column 2 Column 3
1 abc definition ghi
2 jkl mno pqr
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!