简体   繁体   中英

How to save data if the status is not active using mysql for vb.net

this is the first few days of my coding journey. For this code, Im trying to add a product in a table but I want to use a condition that will only allow it to add if the status of the product is not active or if its a new entry.

Public Sub checkitem2()

    Dim StatDel, StatAc, ProdCode, ProdStat As String

    StatDel = "DELETED"
    StatAc = "ACTIVE"

    strSQL = "SELECT * FROM tbl_products where prod_code = '" & TextBox1.Text & "'"

    cmd = New MySqlCommand(strSQL, con)
    con.Open()
    dr = cmd.ExecuteReader()

    If dr.Read() Then

        ProdCode = (dr.Item("prod_code").ToString())
        ProdStat = (dr.Item("prod_status").ToString())

        If TextBox1.Text = ProdCode And ProdStat = StatAc Then

            MessageBox.Show("FOUND ACTIVE")

        Else

            MessageBox.Show("FOUND " + ProdStat)
            'additems()

        End If

    End If

    con.Close()
    cmd.Dispose()
    dr.Close()

End Sub

这是我的表格的图片

` Public Sub additem()

    con.Close()
    cmd.Dispose()
    dr.Close()

        If TextBox2.Text = Nothing Then
            MessageBox.Show("Product Name must not be empty", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            TextBox2.Focus()
        Else
            cmd.CommandType = System.Data.CommandType.Text
            cmd.Connection = con
        con.Open()

            'ADD NEW DATA TO TABLE
            cmd.CommandText = "INSERT INTO tbl_products(prod_code,prod_name,prod_price,prod_weight,prod_brand,prod_manufacturer,prod_dateadded,prod_remarks,prod_status) " & _
            " VALUES ('" & Me.TextBox1.Text & "', '" & Me.TextBox2.Text & "', '" & Me.TextBox3.Text & "', '" & Me.TextBox4.Text & "', '" & Me.ComboBox1.Text & "', '" & Me.ComboBox2.Text & "', '" & dateNow & "', '" & Me.TextBox5.Text & "', '" & "ACTIVE" & "')"

        cmd.Connection = con
        cmd.ExecuteNonQuery()
        MessageBox.Show("Succesfully added: " & TextBox1.Text, "Info", MessageBoxButtons.OK)
        TextBox2.Clear()
        TextBox5.Clear()
        TextBox1.SelectAll()
        TextBox1.Focus()
        con.Close()
        TextBox3.Text = "0"
        TextBox4.Text = "0"
        loadcomboboxes()

        End If

    con.Close()
    dr.Close()
    cmd.Dispose()

        ' frm_Grid.prodlist()
        ' Application.Restart()

End Sub`

I have tidied up your code a bit.

There are 2 important things to consider when working with a database.

  1. Make sure your connections are closed. To accomplish this you need to keep you connection local to the method where it is used. Using...End Using blocks ensure that your database objects are closed and disposed even if there is an error. In this code the command and connection are both included in the Using.

  2. Always use parameters. This protects your database from sql injection and helps avoid mistakes in types. I had to guess at the datatype and field size in your database when I added the parameters. Check for the correct type an size.

If you have questions about this code just post a comment.

Private ConStr As String = "Your connection string"

Public Function checkitem2() As Boolean
    Dim ProdStat As Object
    Dim strSQL = "SELECT prod_status FROM tbl_products where prod_code = @ProductCode;"
    Using con As New MySqlConnection(ConStr),
            cmd As New MySqlCommand(strSQL, con)
        cmd.Parameters.Add("@ProductCode", MySqlDbType.Int32).Value = CInt(TextBox1.Text)
        con.Open()
        ProdStat = cmd.ExecuteScalar
    End Using
    If Not ProdStat Is Nothing AndAlso ProdStat.ToString = "Active" Then
        Return False
    Else
        Return True
    End If
End Function

Private Sub InsertData()
    'Validation
    If Not checkitem2() Then
        MessageBox.Show("This item exists as Active")
        Exit Sub
    End If
    'The .Text property will not be Nothing. It is an empty String
    If String.IsNullOrEmpty(TextBox2.Text) Then
        MessageBox.Show("Product Name must not be empty", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        TextBox2.Focus()
        Exit Sub
    End If
    Dim sql = "INSERT INTO tbl_products(prod_code,prod_name,prod_price,prod_weight,prod_brand,prod_manufacturer,prod_dateadded,prod_remarks,prod_status) 
               VALUES (@Code, @Name, @Price, @Weight, @Brand, @Manufacturer, @DateAdded, @Remarks, 'ACTIVE');"
    Using con As New MySqlConnection(ConStr),
            cmd As New MySqlCommand(sql, con)
        With cmd.Parameters
            .Add("@Code", MySqlDbType.Int32).Value = CInt(TextBox1.Text)
            .Add("@Name", MySqlDbType.VarChar, 100).Value = TextBox2.Text
            .Add("@Price", MySqlDbType.Decimal).Value = CDec(TextBox3.Text)
            .Add("@Weight", MySqlDbType.Int32).Value = CInt(TextBox4.Text)
            .Add("@Brand", MySqlDbType.VarChar, 100).Value = ComboBox1.Text
            .Add("@Manufacturer", MySqlDbType.VarChar, 100).Value = ComboBox2.Text
            .Add("@DateAdded", MySqlDbType.DateTime).Value = Now
            .Add("@Remarks", MySqlDbType.VarChar, 300).Value = TextBox5.Text
        End With
        con.Open()
        cmd.ExecuteNonQuery()
    End Using
    MessageBox.Show("Succesfully added: " & TextBox1.Text, "Info", MessageBoxButtons.OK)
    TextBox2.Clear()
    TextBox5.Clear()
    TextBox1.SelectAll()
    TextBox1.Focus()
    TextBox3.Text = "0"
    TextBox4.Text = "0"
    loadcomboboxes()
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM