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

        showdata()
        ShowList()
        setGrid()
        txtQTY.Value = "1"
        txtUnit.Text = "100"
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        Main2.Show()
        Me.Hide()
    End Sub
    Private Sub showdata()

       Dim dt As New DataTable
        gsql = "Select * from SHARE"

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

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

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

        End With
        dt = Nothing
    End Sub
    Private Sub setGrid()
        If dgvshare.RowCount > 0 Then
            dgvshare.Columns(0).HeaderText = "รหัสหุ้น"
            dgvshare.Columns(1).HeaderText = "รหัสสมาชิก"
            dgvshare.Columns(2).HeaderText = "จำนวนหุ้น"
            dgvshare.Columns(3).HeaderText = "บาทต่อหุ้น"
            dgvshare.Columns(4).HeaderText = "ราคารวมหุ้น"
        End If
    End Sub

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

            gsql = "insert into SHARE  Values ("
            gsql += "'" & txtid.Text & "',"
            gsql += "'" & txtidM.Text & "',"
            gsql += "'" & txtQTY.Text & "',"
            gsql += "'" & txtUnit.Text & "',"
            gsql += "'" & txtTotel.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 SHARE Where Share_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 M_ID = '" & txtidM.Text & "',"
        gsql += "Share_QTY = '" & txtQTY.Text & "',"
        gsql += "Share_Unit = '" & txtUnit.Text & "',"
        gsql += "Share_Totel = '" & txtTotel.Text & "'"
        gsql += " Where Share_ID = '" & txtid.Text & "'"
        With cmd

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

        txtid.Text = ""
        txtidM.Text = ""
        txtQTY.Text = ""
        txtUnit.Text = ""
        txtTotel.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 SHARE where Share_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("Share_ID").ToString
            txtidM.Text = dr.Item("M_ID").ToString
            txtQTY.Text = dr.Item("Share_QTY").ToString
            txtUnit.Text = dr.Item("Share_Unit").ToString
            txtTotel.Text = dr.Item("Share_Totel").ToString


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

        End If
        dr.Close()
        btnSearch.Text = ""
    End Sub
    Private Sub ClaerText()
        txtid.Text = ""
        txtidM.Text = ""
        txtQTY.Text = ""
        txtUnit.Text = ""
        txtTotel.Text = ""
    End Sub


    Private Sub btncalculate_Click(sender As Object, e As EventArgs) Handles btncalculate.Click


        txtTotel.Text = txtQTY.Value * txtUnit.Text


    End Sub
End Class