Imports System.Data.OleDb
Public Class frmNewCustomer

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

        btnDelete.Enabled = False
        txtLastName.Enabled = True
        txtFirstName.Enabled = True
        txtMiddleName.Enabled = True
        txtAge.Enabled = True
        txtAddress.Enabled = True
        txtContactNo.Enabled = True
        cmbGender.Enabled = True
        cmbCategory.Enabled = True
    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 CustomerInfo set Category = '" & cmbCategory.Text & "', LastName = '" & txtLastName.Text & "', FirstName =  '" & txtFirstName.Text & "', MiddleName =  '" & txtMiddleName.Text & "', Address =  '" & txtAddress.Text & "', Birthday = '" & DateTimePicker1.Text & "', Age = '" & txtAge.Text & "',  Gender = '" & cmbGender.Text & "', ContactNo = '" & txtContactNo.Text & "'  where CustomerNo = '" & txtCustomerNo.Text & "'"
            '  str = "Update CustomerInfo set CustomerName = '" & txtCustomerName.Text & "' where CustomerNo = '" & txtCustomerNo.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 CustomerInfo", conn)
            da = New OleDbDataAdapter(cmd)
            ds.Clear()
            da.Fill(ds, "CustomerInfo")
            DataGridView1.DataSource = ds.Tables("CustomerInfo")
            '---------------------------------------------------------
            '  conn.Close()

            txtCustomerNo.Enabled = False
            txtLastName.Enabled = False
            txtFirstName.Enabled = False
            txtMiddleName.Enabled = False
            txtAge.Enabled = False
            txtAddress.Enabled = False
            txtContactNo.Enabled = False
            cmbGender.Enabled = False
            cmbCategory.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 CustomerInfo where CustomerNo = '" & txtCustomerNo.Text & "'"
                cmd = New OleDbCommand(str, conn)
                cmd.ExecuteNonQuery()
                MsgBox("Record Deleted!")
                txtCustomerNo.Text = ""
                txtLastName.Text = ""
                txtFirstName.Text = ""
                txtMiddleName.Text = ""
                txtAddress.Text = ""
                txtAge.Text = ""
                cmbGender.Text = ""
                cmbCategory.Text = ""
                txtContactNo.Text = ""

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

                cmd = New OleDbCommand("select * from CustomerInfo", conn)
                da = New OleDbDataAdapter(cmd)
                ds.Clear()
                da.Fill(ds, "CustomerInfo")
                DataGridView1.DataSource = ds.Tables("CustomerInfo")
                '---------------------------------------------------------
                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 frmNewCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtAge.MaxLength = 2 '// set max. allowed characters.

        txtCustomerNo.Enabled = False
        txtLastName.Enabled = False
        txtFirstName.Enabled = False
        txtLastName.Enabled = False
        txtMiddleName.Enabled = False
        txtAge.Enabled = False
        txtAddress.Enabled = False
        txtContactNo.Enabled = False
        cmbGender.Enabled = False
        cmbCategory.Enabled = False

        btnUpdate.Enabled = False


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

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


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        DataGridView1.Refresh()
    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
        txtCustomerNo.Text = DataGridView1.Item(0, i).Value
        cmbCategory.Text = DataGridView1.Item(1, i).Value
        txtLastName.Text = DataGridView1.Item(2, i).Value
        txtFirstName.Text = DataGridView1.Item(3, i).Value
        txtMiddleName.Text = DataGridView1.Item(4, i).Value
        txtAddress.Text = DataGridView1.Item(5, i).Value
        DateTimePicker1.Text = DataGridView1.Item(6, i).Value
        txtAge.Text = DataGridView1.Item(7, i).Value
        cmbGender.Text = DataGridView1.Item(8, i).Value
        txtContactNo.Text = DataGridView1.Item(9, i).Value

        conn = New OleDbConnection(cn)

    End Sub
End Class