Imports System.Data
Imports System.Data.OleDb
Public Class addem
    Dim dr As OleDbDataReader
    Dim cmd As New OleDbCommand
    Dim gSQL As String
    Dim total As Integer
    Private Sub addem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call connectdatabase()
        Call GetDocNo()
        Call Cleartext()
    End Sub
    'Private Sub fillcbofood()
    '    gSQL = "Select * From Foods_Train"
    '    Dim da As New OleDbDataAdapter(gSQL, cnn)
    '    Dim dt As New DataTable
    '    da.Fill(dt)
    '    With cbofood
    '        .DataSource = dt
    '        .DisplayMember = dt.Columns.Item("Food_name").ColumnName
    '        .ValueMember = dt.Columns.Item("Food_ID").ColumnName
    '    End With
    'End Sub
    'Private Sub fillcboIns()
    '    gSQL = "Select * From Institution_Train"
    '    Dim da As New OleDbDataAdapter(gSQL, cnn)
    '    Dim dt As New DataTable
    '    da.Fill(dt)
    '    With cbIsn
    '        .DataSource = dt
    '        .DisplayMember = dt.Columns.Item("Ins_name").ColumnName
    '        .ValueMember = dt.Columns.Item("Ins_ID").ColumnName
    '    End With
    'End Sub
    'Private Sub fillcboPlace()
    '    gSQL = "Select * From Place_Train"
    '    Dim da As New OleDbDataAdapter(gSQL, cnn)
    '    Dim dt As New DataTable
    '    da.Fill(dt)
    '    With cboplace
    '        .DataSource = dt
    '        .DisplayMember = dt.Columns.Item("Place_name").ColumnName
    '        .ValueMember = dt.Columns.Item("Place_ID").ColumnName
    '    End With
    'End Sub
    'Private Sub fillcboNar()
    '    gSQL = "Select * From Narretor_Train"
    '    Dim da As New OleDbDataAdapter(gSQL, cnn)
    '    Dim dt As New DataTable
    '    da.Fill(dt)
    '    With cbonar
    '        .DataSource = dt
    '        .DisplayMember = dt.Columns.Item("Nar_name").ColumnName
    '        .ValueMember = dt.Columns.Item("Nar_ID").ColumnName
    '    End With
    'End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim total As Integer
        Dim aa As Integer
        Dim bb As Integer
        Dim cc As Integer
        Dim dd As Integer
        Dim ee As Integer

        aa = TextBox1.Text
        bb = TextBox2.Text
        cc = TextBox3.Text
        dd = TextBox4.Text
        ee = TextBox5.Text

        total = (aa + bb + cc + dd + ee)
        Label2.Text = total

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Trian.Show()
        Me.Hide()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        Label2.Text = ""

    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        _11.Show()
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        _11P.Show()
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        _11F.Show()
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        _11N.Show()
    End Sub
    Private Sub GetDocNo()
        gSQL = "Select * From RunDoc Where DocNo = 'TDT'"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            dr = .ExecuteReader()
        End With
        dr.Read()
        If dr.HasRows Then
            Label1.Text = dr.Item("RunNo") + 1
        Else

        End If
        dr.Close()
    End Sub
    Private Sub UpdateDocNo()
        gSQL = "Update RunDoc Set RunNo = '" & Label1.Text & "'"
        gSQL += " Where DocNo = 'TDT'"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            .ExecuteNonQuery()
        End With
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        gSQL = "Insert Into Train_detail Values ("
        gSQL += "'" & Label1.Text & "',"
        gSQL += "'" & no.Text & "',"
        gSQL += "'" & TextBox6.Text & "',"
        gSQL += "'" & TextBox7.Text & "',"
        gSQL += "'" & TextBox8.Text & "',"
        gSQL += "'" & TextBox9.Text & "',"
        gSQL += "'" & TextBox1.Text & "',"
        gSQL += "'" & TextBox2.Text & "',"
        gSQL += "'" & TextBox3.Text & "',"
        gSQL += "'" & TextBox4.Text & "',"
        gSQL += "'" & TextBox5.Text & "',"
        gSQL += "'" & Label2.Text & "')"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            .ExecuteNonQuery()
        End With
        MessageBox.Show("เพิ่มรายละเอียดการอบรมใหม่แล้ว", "ไม่สามารถเพิ่มข้อมูลได้", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Call UpdateDocNo()
        Call GetDocNo()

    End Sub

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

    End Sub
    Private Sub Cleartext()
        no.Text = ""
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        TextBox9.Text = ""
        Label2.Text = ""
        GetDocNo()

    End Sub
End Class