Imports System.Data
Imports System.Data.SqlClient
Public Class Employee
Private ConnectionString As String = "Data Source=TRUEFASTER\SQLEXPRESS;Initial Catalog=OrderingRestaurant;Integrated Security=True"
Dim SqlConnection As New SqlConnection(ConnectionString)
Dim SqlCommand As New SqlCommand
Dim da As New SqlDataAdapter
Dim IsFine As Boolean = False
Dim btnChk As String
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
btnChk = "Add" 'เก็บการคิกปุ่มโดยให้มีค่าเป็นAdd
btnInsert.Enabled = True 'ปุ่มเพิ่มปิดการทำงาน
btnUpdate.Enabled = True 'ปุ่มแก้ไขปิดการทำงาน
btnDelete.Enabled = True 'ปุ่มลบปิดการทำงาน
btnSave.Enabled = True 'ปุ่มตกลงปิดการทำงาน
btnCancel.Enabled = True 'ปุ่มยกเลิกปิดการทำงาน
txtIDEmployee.Text = ""
txtNameEmployee.Text = ""
Sex.Text = ""
txtAddress.Text = ""
txtTel.Text = ""
txtPass.Text = ""
txtSalary.Text = ""
txtemail.Text = ""
txtPID.SelectedValue = ""
txtNameEmployee.Focus() 'กำหนดให้ทำงานที่ txtNameEmployee
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If txtIDEmployee.Text = "" Or txtNameEmployee.Text = "" Or txtSalary.Text = "" Or txtAddress.Text = "" Or txtTel.Text = "" Or txtemail.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมุลพนักงานให้ครบ", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
Try
Dim SqlInsert As String = "INSERT INTO Employee(Employee_ID, Name, Sex, Salary, Address, Tel, Password, email, Position_ID) VALUES('" & txtIDEmployee.Text & "','" & txtNameEmployee.Text & "','" & Sex.Text & "','" & CInt(txtSalary.Text) & "','" & txtAddress.Text & "','" & CInt(txtTel.Text) & "','" & txtPass.Text & "','" & txtemail.Text & "','" & txtPID.SelectedValue & "')"
Dim SqlConnection As New SqlConnection(ConnectionString)
SqlConnection.Open()
Dim SqlCommand As New SqlCommand(SqlInsert, SqlConnection)
SqlCommand.ExecuteNonQuery()
MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว", "ผลการทำงาน")
SqlConnection.Close()
btnUpdate.Enabled = True
btnDelete.Enabled = True
ClearControl()
RefreshDataGrid()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Private Sub ClearControl()
txtIDEmployee.Text = ""
txtNameEmployee.Text = ""
Sex.Text = ""
txtSalary.Text = ""
txtAddress.Text = ""
txtTel.Text = ""
txtPass.Text = ""
txtemail.Text = ""
txtPID.Text = ""
End Sub
Private Sub RefreshDataGrid()
Dim SqlOrderingRestaurant As String
SqlOrderingRestaurant = "SELECT Employee_ID, "
SqlOrderingRestaurant &= "Name, Sex, "
SqlOrderingRestaurant &= "Salary, Address, "
SqlOrderingRestaurant &= "Tel, Password, "
SqlOrderingRestaurant &= "email, "
SqlOrderingRestaurant &= "Position_ID "
SqlOrderingRestaurant &= "FROM Employee "
If IsFine = True Then
OrderingRestaurantDataSet.Tables("Employee").Clear()
End If
da = New SqlDataAdapter(SqlOrderingRestaurant, ConnectionString)
da.Fill(OrderingRestaurantDataSet, "Employee")
If OrderingRestaurantDataSet.Tables("Employee").Rows.Count <> 0 Then
IsFine = True
With DataGridView1
.ReadOnly = True
.DataSource = OrderingRestaurantDataSet.Tables("Employee")
End With
Else
IsFine = False
End If
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
btnChk = "Update" 'เก็บการคิกปุ่มโดยให้มีค่าเป็น Update
txtIDEmployee.Text = ""
txtNameEmployee.Text = ""
Sex.Text = ""
txtAddress.Text = ""
txtTel.Text = ""
txtSalary.Text = ""
txtemail.Text = ""
txtPID.Text = ""
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim SqlDelete As String
Dim SqlConnection As New SqlConnection(ConnectionString)
If MessageBox.Show("ต้องการลบข้อมูลนี้", "ยืนยัน", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
'คำสั่งลบ SQL ตามคำสั่งพนักงาน
SqlDelete = "DELETE FROM Employee"
SqlDelete &= " WHERE(Employee_ID = '" & txtIDEmployee.Text & "')"
Dim SqlCommand As New SqlCommand(SqlDelete, SqlConnection)
SqlConnection.Open()
With SqlCommand
.CommandType = CommandType.Text
.CommandText = SqlDelete
.Connection = SqlConnection
.ExecuteNonQuery() ติดตรงนี้คะ
End With
SqlConnection.Close()
MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว", "ผลกาารทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ClearControl()
RefreshDataGrid()
End If
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
btnChk = "" 'เก็บค่าการคิกปุ่มโดยให้มีค่าเป็นค่าว่าง
btnInsert.Enabled = True 'ปุ่มเพิ่มเปิดการทำงาน
btnUpdate.Enabled = True 'ปุ่มแก้ไขเปิดการทำงาน
btnDelete.Enabled = True 'ปุ่มลบเปิดการทำงาน
btnSave.Enabled = True 'ปุ่มตกลงเปิดการทำงาน
btnCancel.Enabled = True 'ปุ่มยกเลิกเปิดการทำงาน
End Sub
Private Sub Employee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'OrderingRestaurantDataSet.Position' table. You can move, or remove it, as needed.
Me.PositionTableAdapter.Fill(Me.OrderingRestaurantDataSet.Position)
'TODO: This line of code loads data into the 'OrderingRestaurantDataSet.DataTable1' table. You can move, or remove it, as needed.
Me.DataTable1TableAdapter.Fill(Me.OrderingRestaurantDataSet.DataTable1)
End Sub
End Class