Imports System.Data.OleDb

Public Class frmEditProduct


    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        btnUpdate.Enabled = True

        btnDelete.Enabled = False
        '    txtProductCategory.Enabled = True
        txtProductName.Enabled = True
        txtProductQuantity.Enabled = True
     
    End Sub

    Private Sub frmEditProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtProductNo.Enabled = False
        txtProductCategory.Enabled = False
        txtProductName.Enabled = False
        txtProductQuantity.Enabled = False

       

        conn = New OleDbConnection(cn)
        Try
            ' ORIG ------------------------------------------------------------
            conn.Open()

            cmd = New OleDbCommand("select * from ProductInfo", conn)
            da = New OleDbDataAdapter(cmd)
            ds.Clear()
            da.Fill(ds, "ProductInfo")
            DataGridView1.DataSource = ds.Tables("ProductInfo")
            '---------------------------------------------------------
            conn.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        DataGridView1.Refresh()

    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        conn = New OleDbConnection(cn)
        Try
            conn.Open()
            str = "Update ProductInfo set Category = '" & txtProductCategory.Text & "', ProductName = '" & txtProductName.Text & "', Quantity =  '" & txtProductQuantity.Text & "'"

            cmd = New OleDbCommand(str, conn)
            cmd.ExecuteNonQuery()
            'conn.Close()
            MsgBox("Record Updated Successfully!")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        'conn.Close()
        Try
            ' ORIG ------------------------------------------------------------
            '    conn.Open()

            cmd = New OleDbCommand("select * from ProductInfo", conn)
            da = New OleDbDataAdapter(cmd)
            ds.Clear()
            da.Fill(ds, "ProductInfo")
            DataGridView1.DataSource = ds.Tables("ProductInfo")
            '---------------------------------------------------------
            '  conn.Close()

            txtProductNo.Enabled = False
            txtProductCategory.Enabled = False
            txtProductName.Enabled = False
            txtProductQuantity.Enabled = False
          

            btnDelete.Enabled = True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        DataGridView1.Refresh()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'Delete
        Dim i As Integer
        i = MsgBox("Are you sure you want to delete the record?", vbYesNo, "Sweat and Shape Gym")
        If i = vbYes Then
            conn = New OleDbConnection(cn)
            Try
                conn.Open()
                str = "Delete from ProductInfo where CustomerNo = '" & txtProductNo.Text & "'"
                cmd = New OleDbCommand(str, conn)
                cmd.ExecuteNonQuery()
                MsgBox("Record Deleted!")
                txtProductNo.Text = ""
                txtProductCategory.Text = ""
                txtProductName.Text = ""
                txtProductQuantity.Text = ""
             

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            'conn.Close()
            Try
                ' ORIG ------------------------------------------------------------
                'conn.Open()

                cmd = New OleDbCommand("select * from ProductInfo", conn)
                da = New OleDbDataAdapter(cmd)
                ds.Clear()
                da.Fill(ds, "ProductInfo")
                DataGridView1.DataSource = ds.Tables("ProductInfo")
                '---------------------------------------------------------
                conn.Close()


            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            DataGridView1.Refresh()
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()

    End Sub
    Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
        'Putting infos from dbase to textboxes
        Dim i As Integer
        i = DataGridView1.CurrentRow.Index
        txtProductNo.Text = DataGridView1.Item(0, i).Value
        txtProductCategory.Text = DataGridView1.Item(1, i).Value
        txtProductName.Text = DataGridView1.Item(2, i).Value
        txtProductQuantity.Text = DataGridView1.Item(3, i).Value
      

        conn = New OleDbConnection(cn)

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub txtProductCategory_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtProductCategory.TextChanged

    End Sub
End Class