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

        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 COMPANY"

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

        End With
    End Sub
    Private Sub ShowList()
        Dim dt As New DataTable
        gsql = "select * from COMPANY"

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

        End With
        dt = Nothing
    End Sub
    Private Sub setGrid()
        If dgvcompany.RowCount > 0 Then
            dgvcompany.Columns(0).HeaderText = "รหัสบริษัท"
            dgvcompany.Columns(1).HeaderText = "ชื่อบริษัท "
            dgvcompany.Columns(2).HeaderText = "ที่อยู่"
            dgvcompany.Columns(3).HeaderText = "เบอร์โทรศัพท์"
        End If
    End Sub

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

            gsql = "Insert Into COMPANY Values ("
            gsql += "'" & txtid.Text & "',"
            gsql += "'" & txtName.Text & "',"
            gsql += "'" & txtAddress.Text & "',"
            gsql += "'" & txttel.Text & "')"

            With cmd
                .CommandType = CommandType.Text
                .CommandText = gsql
                .Connection = cnn
                .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 COMPANY Where CPY_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 COMPANY Set CPY_Name = '" & txtName.Text & "',"
        gsql += "CPY_tel = '" & txttel.Text & "',"
        gsql += "CPY_Address = '" & txtAddress.Text & "'"

        gsql += " Where CPY_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 = ""
        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 COMPANY where CPY_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("CPY_ID").ToString
            txtName.Text = dr.Item("CPY_Name").ToString
            txtAddress.Text = dr.Item("CPY_Address").ToString
            txttel.Text = dr.Item("CPY_Tel").ToString
           
        Else
            MessageBox.Show("ไม่พบข้อมูล")

        End If
        dr.Close()


        btnSearch.Text = ""
    End Sub
    Private Sub ClaerText()
        txtid.Text = ""
        txtName.Text = ""
        txtAddress.Text = ""
        txttel.Text = ""
    End Sub
End Class