Imports System.Data
Imports System.Data.OleDb
Public Class FmType
    Dim dr As OleDbDataReader
    Dim cmd As New OleDbCommand
    Dim gsql As String
    Private Sub FmType_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 TYPE"

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

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

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

        End With
        dt = Nothing
    End Sub
    Private Sub setGrid()
        If dgvtype.RowCount > 0 Then
            dgvtype.Columns(0).HeaderText = "รหัสประเภท"
            dgvtype.Columns(1).HeaderText = "ชื่อประเภท "
        End If
    End Sub

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

            gsql = "Insert Into TYPE Values ("
            gsql += "'" & txtid.Text & "',"
            gsql += "'" & txtName.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 TYPE Where TP_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 TYPE Set TP_Name = '" & txtName.Text & "'"
        gsql += " Where TP_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 = ""
        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 TYPE where TP_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("TP_ID").ToString
            txtName.Text = dr.Item("TP_Name").ToString
        Else
            MessageBox.Show("ไม่พบข้อมูล")

        End If
        dr.Close()
        txtSearch.Text = ""
    End Sub
    Private Sub ClaerText()
        txtid.Text = ""
        txtName.Text = ""
    End Sub

    Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click

    End Sub
End Class