Imports System.Data
Imports System.Data.OleDb
Public Class FmEmployee
    Dim dr As OleDbDataReader
    Dim cmd As New OleDbCommand
    Dim gsql As String
    Private Sub FmEmployee_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ConnectDatabase()
        Label8.Text = Today

        showdata()
        ShowList()
        setGrid()

        
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Main2.Show()
        Me.Hide()
    End Sub
    Private Sub showdata()
        Dim dt As New DataTable
        gsql = "Select * from EMPLOYEE"

        With cmd
            .CommandType = CommandType.Text
            .CommandText = gsql
            .Connection = cnn
            dr = .ExecuteReader
            If dr.HasRows Then
                dt.Load(dr)
                dgvemployee.DataSource = dt
            End If
            dr.Close()

        End With

    End Sub
    Private Sub ShowList()
        Dim dt As New DataTable
        gsql = "Select * from EMPLOYEE"

        With cmd
            .CommandType = CommandType.Text
            .CommandText = gsql
            .Connection = cnn
            dr = .ExecuteReader
            If dr.HasRows Then
                dt.Load(dr)
                dgvemployee.DataSource = dt
            End If
            dr.Close()

        End With
        dt = Nothing


    End Sub
    Private Sub setGrid()
        If dgvemployee.RowCount > 0 Then
            dgvemployee.Columns(0).HeaderText = "รหัสพนักงาน"
            dgvemployee.Columns(1).HeaderText = "ชื่อสกุล "
            dgvemployee.Columns(2).HeaderText = "เลขบัตรประชาชน"
            dgvemployee.Columns(3).HeaderText = "ที่อยู่"
            dgvemployee.Columns(4).HeaderText = "เบอร์โทร"
        End If
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Try

            gsql = "insert into EMPLOYEE  Values ("
            gsql += "'" & txtid.Text & "',"
            gsql += "'" & txtname.Text & "',"
            gsql += "'" & txtidcard.Text & "',"
            gsql += "'" & txtAddress.Text & "',"
            gsql += "'" & txttel.Text & "')"
        

            With cmd
                .CommandType = CommandType.Text
                .Connection = cnn
                .CommandText = gsql
                .ExecuteNonQuery()

            End With
            MessageBox.Show("เพิ่มข้อมุลใหม่แล้ว", "เพิ่มข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As Exception
            MessageBox.Show("มีรหัสพนักงานนี้แล้วไม่สามารถเพิ่มได้", "เพิ่มข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
        showdata()
        ClaerText()

    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Dim ans As MsgBoxResult
        ans = MessageBox.Show("ต้องการลบรายการหรือไม่", "ยืนยันการลบ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If ans = MsgBoxResult.Yes Then
            gsql = "Delete From EMPLOYEE Where EMP_ID = '" & txtid.Text & "'"
            With cmd
                .CommandType = CommandType.Text
                .CommandText = gsql
                .Connection = cnn
                .ExecuteNonQuery()
            End With
            MessageBox.Show("ลบรายการแล้ว", "ลบข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        showdata()
        ClaerText()
    End Sub

    Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
        gsql = "Update EMPLOYEE Set EMP_Name = '" & txtname.Text & "',"
        gsql += "EMP_idcard = '" & txtidcard.Text & "',"
        gsql += "EMP_Address = '" & txtAddress.Text & "',"
        gsql += "EMP_tel = '" & txttel.Text & "'"
        gsql += " Where EMP_ID = '" & txtid.Text & "'"
        With cmd

            .CommandType = CommandType.Text
            .CommandText = gsql
            .Connection = cnn
            .ExecuteNonQuery()
        End With
        MessageBox.Show("แก้ไขรายการใหม่", "แก้ไขข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)

        txtid.Text = ""
        txtname.Text = ""
        txtidcard.Text = ""
        txtAddress.Text = ""
        txttel.Text = ""
        Me.ShowList()

    End Sub

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

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        gsql = "Select * From EMPLOYEE where EMP_ID ='" & txtSearch.Text & "'"

        With cmd
            .CommandType = CommandType.Text
            .CommandText = gsql
            .Connection = cnn
            dr = .ExecuteReader
        End With
        dr.Read()
        If dr.HasRows Then
            txtid.Text = dr.Item("EMP_ID").ToString
            txtname.Text = dr.Item("EMP_Name").ToString
            txtidcard.Text = dr.Item("EMP_idcard").ToString
            txtAddress.Text = dr.Item("EMP_Address").ToString
            txttel.Text = dr.Item("EMP_Tel").ToString
            

        Else
            MessageBox.Show("ไม่พบข้อมูล")

        End If
        dr.Close()


        btnSearch.Text = ""
    End Sub
    Private Sub ClaerText()
        txtid.Text = ""
        txtname.Text = ""
        txtidcard.Text = ""
        txtAddress.Text = ""
        txttel.Text = ""
    End Sub

 
End Class