Imports System.Data
Imports System.Data.OleDb

Public Class Narretor
    Dim dr As OleDbDataReader
    Dim cmd As New OleDbCommand
    Dim gSQL As String

    Private Sub Narretor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call connectdatabase()
        Call Cleartext()
        Call GetDocNo()

    End Sub
    Private Sub GetDocNo()
        gSQL = "Select * From RunDoc Where DocNo = 'NAR'"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            dr = .ExecuteReader()
        End With
        dr.Read()
        If dr.HasRows Then
            narNo.Text = dr.Item("RunNo") + 1
        Else

        End If
        dr.Close()
    End Sub
    Private Sub UpdateDocNo()
        gSQL = "Update RunDoc Set RunNo = '" & narNo.Text & "'"
        gSQL += " Where DocNo = 'NAR'"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            .ExecuteNonQuery()
        End With
    End Sub
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        gSQL = "select * from Narretor_Train where Nar_ID  = '" & txtSearch.Text & "'"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            dr = .ExecuteReader
        End With
        dr.Read()
        If dr.HasRows Then
            narNo.Text = dr.Item("Nar_ID").ToString
            narname.Text = dr.Item("Nar_name").ToString
            nargeniuse.Text = dr.Item("Nar_genius").ToString
            narstatus.Text = dr.Item("Nar_Status").ToString
            narprice.Text = dr.Item("Nar_price").ToString
            naradd.Text = dr.Item("Nar_address").ToString
            nartel.Text = dr.Item("Nar_tel").ToString
            naremail.Text = dr.Item("Nar_email").ToString

        Else
            MessageBox.Show("ไม่พบข้อมูล")
            Call Cleartext()
        End If
        dr.Close()
    End Sub

    Private Sub PictureBox6_Click(sender As Object, e As EventArgs) Handles PictureBox6.Click
        gSQL = "Insert Into Narretor_Train Values ("
        gSQL += "'" & narNo.Text & "',"
        gSQL += "'" & narname.Text & "',"
        gSQL += "'" & nargeniuse.Text & "',"
        gSQL += "'" & narstatus.Text & "',"
        gSQL += "'" & narprice.Text & "',"
        gSQL += "'" & naradd.Text & "',"
        gSQL += "'" & nartel.Text & "',"
        gSQL += "'" & naremail.Text & "')"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            .ExecuteNonQuery()
        End With
        MessageBox.Show("เพิ่มรายการใหม่แล้ว", "เพิ่มข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Call UpdateDocNo()
        Call Cleartext()
    End Sub

    Private Sub PictureBox7_Click(sender As Object, e As EventArgs) Handles PictureBox7.Click
        gSQL = "Update Narretor_Train Set Nar_name ='" & narname.Text & "',"
        gSQL += "Nar_genius='" & nargeniuse.Text & "',"
        gSQL += "Nar_Status='" & narstatus.Text & "',"
        gSQL += "Nar_price='" & narprice.Text & "',"
        gSQL += "Nar_address='" & naradd.Text & "',"
        gSQL += "Nar_tel='" & nartel.Text & "',"
        gSQL += "Nar_email='" & naremail.Text & "'"
        gSQL += " Where Nar_ID ='" & narNo.Text & "'"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            .ExecuteNonQuery()
        End With
        MessageBox.Show("แก้ไขรายการใหม่", "แก้ไขข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Call Cleartext()
    End Sub

    Private Sub PictureBox8_Click(sender As Object, e As EventArgs) Handles PictureBox8.Click
        Dim ans As MsgBoxResult
        ans = MessageBox.Show("ต้องการลบรายการหรือไม่", "ยืนยันการลบ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If ans = MsgBoxResult.Yes Then
            gSQL = "Delete From Narretor_Train where Nar_ID  = '" & txtSearch.Text & "'"
            With cmd
                .CommandType = CommandType.Text
                .CommandText = gSQL
                .Connection = cnn
                .ExecuteNonQuery()
            End With
            MessageBox.Show("ลบรายการแล้ว", "ลบข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Call Cleartext()
        End If

    End Sub
    Private Sub Cleartext()

        narNo.Text = ""
        narname.Text = ""
        nargeniuse.Text = ""
        narstatus.Text = ""
        narprice.Text = ""
        naradd.Text = ""
        nartel.Text = ""
        naremail.Text = ""
    End Sub

    Private Sub PictureBox9_Click(sender As Object, e As EventArgs) Handles PictureBox9.Click
        DTbase.Show()
        Hide()
    End Sub
End Class