001.
Imports
System.Data
002.
Imports
System.Data.SqlClient
003.
Public
Class
Employee
004.
Private
ConnectionString
As
String
=
"Data Source=TRUEFASTER\SQLEXPRESS;Initial Catalog=OrderingRestaurant;Integrated Security=True"
005.
Dim
SqlConnection
As
New
SqlConnection(ConnectionString)
006.
Dim
SqlCommand
As
New
SqlCommand
007.
Dim
da
As
New
SqlDataAdapter
008.
Dim
IsFine
As
Boolean
=
False
009.
Dim
btnChk
As
String
010.
011.
012.
Private
Sub
btnInsert_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnInsert.Click
013.
btnChk =
"Add"
014.
btnInsert.Enabled =
True
015.
btnUpdate.Enabled =
True
016.
btnDelete.Enabled =
True
017.
btnSave.Enabled =
True
018.
btnCancel.Enabled =
True
019.
020.
txtIDEmployee.Text =
""
021.
txtNameEmployee.Text =
""
022.
Sex.Text =
""
023.
txtAddress.Text =
""
024.
txtTel.Text =
""
025.
txtPass.Text =
""
026.
txtSalary.Text =
""
027.
txtemail.Text =
""
028.
txtPID.SelectedValue =
""
029.
txtNameEmployee.Focus()
030.
031.
End
Sub
032.
033.
Private
Sub
btnSave_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnSave.Click
034.
If
txtIDEmployee.Text =
""
Or
txtNameEmployee.Text =
""
Or
txtSalary.Text =
""
Or
txtAddress.Text =
""
Or
txtTel.Text =
""
Or
txtemail.Text =
""
Then
035.
MessageBox.Show(
"กรุณากรอกข้อมุลพนักงานให้ครบ"
,
"คำเตือน"
, MessageBoxButtons.OK, MessageBoxIcon.Warning)
036.
Else
037.
Try
038.
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 &
"')"
039.
040.
Dim
SqlConnection
As
New
SqlConnection(ConnectionString)
041.
SqlConnection.Open()
042.
Dim
SqlCommand
As
New
SqlCommand(SqlInsert, SqlConnection)
043.
SqlCommand.ExecuteNonQuery()
044.
MessageBox.Show(
"เพิ่มข้อมูลเรียบร้อยแล้ว"
,
"ผลการทำงาน"
)
045.
SqlConnection.Close()
046.
btnUpdate.Enabled =
True
047.
btnDelete.Enabled =
True
048.
ClearControl()
049.
RefreshDataGrid()
050.
Catch
ex
As
Exception
051.
MessageBox.Show(ex.Message)
052.
End
Try
053.
054.
End
If
055.
056.
End
Sub
057.
Private
Sub
ClearControl()
058.
txtIDEmployee.Text =
""
059.
txtNameEmployee.Text =
""
060.
Sex.Text =
""
061.
txtSalary.Text =
""
062.
txtAddress.Text =
""
063.
txtTel.Text =
""
064.
txtPass.Text =
""
065.
txtemail.Text =
""
066.
txtPID.Text =
""
067.
068.
End
Sub
069.
Private
Sub
RefreshDataGrid()
070.
Dim
SqlOrderingRestaurant
As
String
071.
SqlOrderingRestaurant =
"SELECT Employee_ID, "
072.
SqlOrderingRestaurant &=
"Name, Sex, "
073.
SqlOrderingRestaurant &=
"Salary, Address, "
074.
SqlOrderingRestaurant &=
"Tel, Password, "
075.
SqlOrderingRestaurant &=
"email, "
076.
SqlOrderingRestaurant &=
"Position_ID "
077.
SqlOrderingRestaurant &=
"FROM Employee "
078.
079.
080.
If
IsFine =
True
Then
081.
OrderingRestaurantDataSet.Tables(
"Employee"
).Clear()
082.
End
If
083.
da =
New
SqlDataAdapter(SqlOrderingRestaurant, ConnectionString)
084.
da.Fill(OrderingRestaurantDataSet,
"Employee"
)
085.
086.
If
OrderingRestaurantDataSet.Tables(
"Employee"
).Rows.Count <> 0
Then
087.
IsFine =
True
088.
With
DataGridView1
089.
.
ReadOnly
=
True
090.
.DataSource = OrderingRestaurantDataSet.Tables(
"Employee"
)
091.
092.
End
With
093.
Else
094.
IsFine =
False
095.
End
If
096.
End
Sub
097.
098.
Private
Sub
btnUpdate_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnUpdate.Click
099.
btnChk =
"Update"
100.
txtIDEmployee.Text =
""
101.
txtNameEmployee.Text =
""
102.
Sex.Text =
""
103.
txtAddress.Text =
""
104.
txtTel.Text =
""
105.
txtSalary.Text =
""
106.
txtemail.Text =
""
107.
txtPID.Text =
""
108.
109.
End
Sub
110.
111.
Private
Sub
btnDelete_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnDelete.Click
112.
Dim
SqlDelete
As
String
113.
Dim
SqlConnection
As
New
SqlConnection(ConnectionString)
114.
115.
If
MessageBox.Show(
"ต้องการลบข้อมูลนี้"
,
"ยืนยัน"
, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK
Then
116.
117.
SqlDelete =
"DELETE FROM Employee"
118.
SqlDelete &=
" WHERE(Employee_ID = '"
& txtIDEmployee.Text &
"')"
119.
120.
Dim
SqlCommand
As
New
SqlCommand(SqlDelete, SqlConnection)
121.
SqlConnection.Open()
122.
123.
With
SqlCommand
124.
.CommandType = CommandType.Text
125.
.CommandText = SqlDelete
126.
.Connection = SqlConnection
127.
.ExecuteNonQuery() ติดตรงนี้คะ
128.
129.
130.
End
With
131.
SqlConnection.Close()
132.
MessageBox.Show(
"ลบข้อมูลเรียบร้อยแล้ว"
,
"ผลกาารทำงาน"
, MessageBoxButtons.OK, MessageBoxIcon.Warning)
133.
ClearControl()
134.
RefreshDataGrid()
135.
136.
End
If
137.
End
Sub
138.
139.
Private
Sub
btnCancel_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnCancel.Click
140.
btnChk =
""
141.
btnInsert.Enabled =
True
142.
btnUpdate.Enabled =
True
143.
btnDelete.Enabled =
True
144.
btnSave.Enabled =
True
145.
btnCancel.Enabled =
True
146.
End
Sub
147.
148.
Private
Sub
Employee_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
149.
150.
Me
.PositionTableAdapter.Fill(
Me
.OrderingRestaurantDataSet.Position)
151.
152.
Me
.DataTable1TableAdapter.Fill(
Me
.OrderingRestaurantDataSet.DataTable1)
153.
154.
End
Sub
155.
End
Class