Option Explicit On
Option Strict On
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Class frmEmployee
Public Com As New SqlCommand
Public dr As SqlDataReader
Dim Conn As New SqlConnection
Dim da As SqlDataAdapter
Dim ds As New DataSet
Dim dt As DataTable
Dim Currentemp_id As String = ""
Dim IsFind As Boolean = False
Dim sup_id As Integer
Dim ImageFileName As String = ""
Dim fs As FileStream
Dim CurrentImage() As Byte
Dim IsFindImage As Boolean = False
Dim IsChangeImage As Boolean = False
Private Sub frmEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToParent()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Dim sqlEmployee As String
sqlEmployee = "SELECT * FROM Employee"
Dim mycom As New SqlCommand
mycom.CommandText = sqlEmployee
mycom.CommandType = CommandType.Text
mycom.Connection = Conn
Dim myDr As SqlDataReader
'สั่งให้ myCom รันคำสั่งเก็บผลลัพธ์ไว้ใน myDr
myDr = mycom.ExecuteReader
'------------------------------------
'สร้าง DataTable ชื่อ myDataTable
Dim myDataTable As New DataTable
'สั่งให้ myDataTable โหลดข้อมูลมาจาก myDr
myDataTable.Load(myDr)
'ปิดการทำงานของ myDr
myDr.Close()
'------------------------------------
'กำหนดรูปแบบของ dataGridView ชื่อ dgvBranchList
dgvEmpList.DataSource = myDataTable
If dgvEmpList.RowCount > 0 Then
dgvEmpList.Columns(0).HeaderText = "รหัสพนักงาน"
dgvEmpList.Columns(1).HeaderText = "ชื่อพนักงาน"
dgvEmpList.Columns(2).HeaderText = "ชื่อเล่น"
dgvEmpList.Columns(3).HeaderText = "ที่อยู่"
dgvEmpList.Columns(4).HeaderText = "เบอร์โทรศัพท์"
dgvEmpList.Columns(5).HeaderText = "วันเข้าทำงาน"
dgvEmpList.Columns(6).HeaderText = "รูปภาพ"
dgvEmpList.Columns(0).Width = 80
dgvEmpList.Columns(1).Width = 165
dgvEmpList.Columns(2).Width = 80
dgvEmpList.Columns(3).Width = 145
dgvEmpList.Columns(4).Width = 80
dgvEmpList.Columns(5).Width = 80
dgvEmpList.Columns(6).Width = 80
End If
dtpWork.Value = Date.Today
'------------------------------------
'กำหนดแหล่งข้อมูลให้กับ BindingSource ชื่อ Bs ให้นำข้อมูลมาจาก myDataTable
bs.DataSource = myDataTable
'------------------------------------
'กำหนดให้ BindingNavigator ผูกติดข้อมูลเข้ากับ Bs
bn.BindingSource = bs
'------------------------------------
'ก่อนทำการ Binding ข้อมูลเข้ากับคอนโทรล ให้ยกเลิกการ Binding ข้อมูลก่อน
txtEmpId.DataBindings.Clear()
txtEmpName.DataBindings.Clear()
txtEmpNickname.DataBindings.Clear()
txtEmpAdd.DataBindings.Clear()
mtbEmpTel.DataBindings.Clear()
dtpWork.DataBindings.Clear()
'picPicture.DataBindings.Clear()
'ทำการ Binding ข้อมูลเข้ากับคอนโทรล TextBox
txtEmpId.DataBindings.Add("Text", bs, "emp_id")
txtEmpName.DataBindings.Add("Text", bs, "emp_name")
txtEmpNickname.DataBindings.Add("Text", bs, "emp_nickname")
txtEmpAdd.DataBindings.Add("Text", bs, "emp_add")
mtbEmpTel.DataBindings.Add("Text", bs, "emp_tel")
dtpWork.DataBindings.Add("Text", bs, "start_emp")
'picPicture.DataBindings.Add("Text", bs, "Picture")
With cboSearch
.Items.Add("รหัสพนักงาน")
.Items.Add("ชื่อพนักงาน")
End With
End Sub
Private Sub ShowAllEmployee() 'แสดงข้อมูลพนักงานทั้งหมด
Dim sqlEmp As String
sqlEmp = "SELECT *FROM Employee"
If IsFind = True Then
ds.Tables("Employee").Clear()
End If
da = New SqlDataAdapter(sqlEmp, Conn)
da.Fill(ds, "Employee")
If ds.Tables("Employee").Rows.Count <> 0 Then
IsFind = True
With dgvEmpList
.ReadOnly = True
.DataSource = ds.Tables("Employee")
End With
'FormatDataGridView()
FormatAllRowsHeight()
Else
IsFind = False
End If
End Sub
'Private Sub FormatDataGridView()
' Dim cs As New DataGridViewCellStyle()
' cs.Font = New Font("Ms Sans Serif", 10, FontStyle.Bold)
' With dgvEmpList
' .ColumnHeadersDefaultCellStyle = cs
' .Columns(0).HeaderText = "รหัสพนักงาน"
' .Columns(1).HeaderText = "ชื่อ นามสกุล"
' .Columns(2).HeaderText = "ชื่อเล่น"
' .Columns(3).HeaderText = "ที่อยู่"
' .Columns(4).HeaderText = "เบอร์โทร"
' .Columns(5).HeaderText = "เข้าทำงาน"
' .Columns(6).HeaderText = "รูป"
' .Columns(0).Width = 80
' .Columns(1).Width = 165
' .Columns(2).Width = 80
' .Columns(3).Width = 145
' .Columns(4).Width = 80
' .Columns(5).Width = 80
' .Columns(6).Width = 80
' .Rows.Item(.Rows.Count - 1).Height = 0
' End With
'End Sub
Private Sub FormatAllRowsHeight()
Dim i As Integer
With dgvEmpList
For i = 0 To .Rows.Count - 2
.Rows.Item(i).Height = 50
Next
End With
End Sub
Private Sub clear()
txtEmpId.Text = " "
txtEmpName.Text = ""
txtEmpNickname.Text = ""
txtEmpAdd.Text = ""
mtbEmpTel.Text = ""
dtpWork.Value = Date.Today
cboSearch.Text = ""
tsSearch.Text = ""
picPicture.Image = picPicture.ErrorImage
End Sub
Private Sub tsSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tsSearch.KeyDown
'เมื่อคีย์ข้อมูลเพื่อ Searth หาข้อมูลที่ต้องการ
If e.KeyCode = Keys.Enter Then
showdata()
End If
End Sub
Private Sub showdata()
Dim sqlsearch As String
sqlsearch = "SELECT * FROM Employee"
Select Case cboSearch.SelectedIndex
Case 0
sqlsearch &= " where (emp_id = @emp_id )"
Case 1
sqlsearch &= " where (emp_name like @emp_name )"
End Select
Dim emp_id As New SqlParameter("emp_id", SqlDbType.NVarChar)
Dim emp_name As New SqlParameter("emp_name", SqlDbType.NVarChar)
Dim mycom As New SqlCommand
With mycom
Select Case cboSearch.SelectedIndex
Case 0
.Parameters.Clear()
.Parameters.Add(emp_id).Value = tsSearch.Text.Trim()
Case 1
.Parameters.Clear()
.Parameters.Add(emp_name).Value = "%" & tsSearch.Text.Trim() & "%"
End Select
.CommandText = sqlsearch
.CommandType = CommandType.Text
.Connection = Conn
Dim dr As SqlDataReader
dr = .ExecuteReader
Dim dt As New DataTable
dt.Load(dr)
dr.Close()
bs.DataSource = dt
bn.BindingSource = bs
End With
End Sub
Private Sub dgvEmpList_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvEmpList.CellMouseUp
'เมื่อดับเบิลคลิกที่เซลล์ในคอนโทรล
If e.RowIndex = dgvEmpList.Rows.Count - 1 Then
Currentemp_id = ""
Exit Sub
End If
txtEmpId.Text = emp_id
txtEmpName.Text = emp_name
txtEmpNickname.Text = emp_nickname
txtEmpAdd.Text = emp_add
mtbEmpTel.Text = emp_tel
dtpWork.Text = start_emp
'If e.RowIndex = dgvEmpList.RowCount - 1 Then
' Exit Sub
'End If
'If e.RowIndex = -1 Then Exit Sub
With dgvEmpList
Currentemp_id = CStr(dgvEmpList.Rows.Item(e.RowIndex).Cells(0).Value)
emp_id = CStr(dgvEmpList.Rows.Item(e.RowIndex).Cells(0).Value)
emp_name = CStr(dgvEmpList.Rows.Item(e.RowIndex).Cells(1).Value)
emp_nickname = CStr(dgvEmpList.Rows.Item(e.RowIndex).Cells(2).Value)
emp_add = CStr(dgvEmpList.Rows.Item(e.RowIndex).Cells(3).Value)
emp_tel = CStr(dgvEmpList.Rows.Item(e.RowIndex).Cells(4).Value)
start_emp = CStr(dgvEmpList.Rows.Item(e.RowIndex).Cells(5).Value)
End With
ShowImage()
End Sub
'แสดงรูปภาพพนักงานคนปัจจุบัน
Private Sub ShowImage()
Dim sqlImage As String
sqlImage = "SELECT Picture FROM Employee"
sqlImage &= " WHERE (emp_id='" & Currentemp_id & "')"
If IsFindImage = True Then
ds.Tables("CurrentImage").Clear()
End If
da.SelectCommand.CommandText = sqlImage
da.Fill(ds, "CurrentImage")
If ds.Tables("CurrentImage").Rows.Count <> 0 Then
IsFindImage = True
CurrentImage = CType(ds.Tables("CurrentImage").Rows(0).Item("Picture"), Byte())
Dim ms As New MemoryStream(CurrentImage, True)
picPicture.Image = Image.FromStream(ms)
Else
IsFindImage = False
End If
End Sub
Private Sub cmdSearth1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearth1.Click
showdata()
End Sub
'การเพิ่มข้อมูลพนักงาน
Private Sub cmdAdd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd1.Click
'หาข้อมูล รหัสตัวสุดท้ายที่มีอยู่ในฐานข้อมูลก่อน เพื่อที่จะบวกเข้าไปอีก 1 จะได้เป็นรหัสใหม่ที่จะ add เข้าไป
Dim sqlGen As String = "SELECT TOP 1 emp_id FROM Employee " & _
"ORDER BY emp_id DESC"
Dim NewId As String
Dim FirstId As String
Dim LastID As Long
With Com
.Connection = Conn
.CommandType = CommandType.Text
.Parameters.Clear()
.CommandText = sqlGen
dr = .ExecuteReader()
If dr.HasRows Then
With dr
.Read()
NewId = .GetString(.GetOrdinal("emp_id"))
End With
FirstId = StringExt.StringFromLeft(NewId, 4)
LastID = CLng(StringExt.StringFromRight(NewId, 4))
LastID += 1
txtEmpId.Text = LastID.ToString("0000")
End If
dr.Close()
End With
If txtEmpName.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมูลพนักงาน !!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtEmpName.Focus()
txtEmpName.SelectAll()
Exit Sub
End If
Dim tmpFileName As String = ""
If ImageFileName <> "" Then
fs = New FileStream(ImageFileName, FileMode.Open, FileAccess.Read)
ReDim CurrentImage(Convert.ToInt32(fs.Length))
fs.Read(CurrentImage, 0, Convert.ToInt32(fs.Length))
fs.Close()
Else
tmpFileName = "c:\tmp.bmp"
picPicture.ErrorImage.Save(tmpFileName)
fs = New FileStream(tmpFileName, FileMode.Open, FileAccess.Read)
ReDim CurrentImage(Convert.ToInt32(fs.Length))
fs.Read(CurrentImage, 0, Convert.ToInt32(fs.Length))
fs.Close()
End If
If MessageBox.Show("คุณต้องการเพิ่มข้อมูลพนักงานใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Try
'ถ้าไม่มีข้อผิดพลาด ให้ทำงานต่างๆ ดังนี้
Dim sqlAdd As String
Dim myCom As New SqlCommand
sqlAdd = "INSERT INTO Employee (emp_id,emp_name,emp_nickname,emp_add,emp_tel,start_emp,Picture)"
sqlAdd &= " VALUES (@emp_id,@emp_name,@emp_nickname,@emp_add,@emp_tel,@start_emp,@Picture)"
With (myCom)
.CommandText = sqlAdd
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.Parameters.Add("@emp_id", SqlDbType.NVarChar).Value = txtEmpId.Text.Trim
.Parameters.Add("@emp_name", SqlDbType.NVarChar).Value = txtEmpName.Text.Trim()
.Parameters.Add("@emp_nickname", SqlDbType.NVarChar).Value = txtEmpNickname.Text.Trim()
.Parameters.Add("@emp_add", SqlDbType.NVarChar).Value = txtEmpAdd.Text.Trim()
.Parameters.Add("@emp_tel", SqlDbType.NVarChar).Value = mtbEmpTel.Text.Trim()
.Parameters.Add("@start_emp", SqlDbType.DateTime).Value = dtpWork.Value.Date
.Parameters.Add("@Picture", SqlDbType.Image).Value = CurrentImage
.ExecuteNonQuery()
End With
If tmpFileName <> "" Then
My.Computer.FileSystem.DeleteFile(tmpFileName)
End If
Catch ex As Exception
'ถ้าเกิดข้อผิดพลาดให้ทำงานดังนี้
MessageBox.Show("เกิดข้อผิดพลาดเนื่องจาก :" & ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
CurrentImage = Nothing
MessageBox.Show("เพิ่มข้อมูลพนักงาน เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
ShowAllEmployee()
clear()
End Sub
'การแก้ไขข้อมูลพนักงาน
Private Sub cmdEdit1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit1.Click
'Dim result As Long
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลพนักงานใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
Exit Sub
End If
Dim tmpFileName As String = ""
If IsChangeImage = True Then
If ImageFileName <> "" Then
fs = New FileStream(ImageFileName, FileMode.Open, FileAccess.Read)
ReDim CurrentImage(Convert.ToInt32(fs.Length))
fs.Read(CurrentImage, 0, Convert.ToInt32(fs.Length))
fs.Close()
Else
tmpFileName = "C:\tmp.bmp"
picPicture.ErrorImage.Save(tmpFileName)
fs = New FileStream(tmpFileName, FileMode.Open, FileAccess.Read)
ReDim CurrentImage(Convert.ToInt32(fs.Length))
fs.Read(CurrentImage, 0, Convert.ToInt32(fs.Length))
fs.Close()
End If
ElseIf IsChangeImage = False Then
tmpFileName = "C:\tmp.bmp"
picPicture.Image.Save(tmpFileName)
fs = New FileStream(tmpFileName, FileMode.Open, FileAccess.Read)
ReDim CurrentImage(Convert.ToInt32(fs.Length))
fs.Read(CurrentImage, 0, Convert.ToInt32(fs.Length))
fs.Close()
End If
Try
Dim sqlEdit As String
sqlEdit = "UPDATE Employee"
sqlEdit &= " SET emp_id=@emp_id,emp_name=@emp_name,emp_nickname=@emp_nickname,emp_add=@emp_add,emp_tel=@emp_tel,start_emp=@start_emp,Picture=@Picture"
sqlEdit &= " WHERE (emp_id=@emp_id)"
Dim myCom As New SqlCommand
With (myCom)
.CommandText = sqlEdit
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.Parameters.Add("@emp_id", SqlDbType.NVarChar).Value = txtEmpId.Text.Trim()
.Parameters.Add("@emp_name", SqlDbType.NVarChar).Value = txtEmpName.Text.Trim()
.Parameters.Add("@emp_nickname", SqlDbType.NVarChar).Value = txtEmpNickname.Text.Trim()
.Parameters.Add("@emp_add", SqlDbType.NVarChar).Value = txtEmpAdd.Text.Trim()
.Parameters.Add("@emp_tel", SqlDbType.NVarChar).Value = mtbEmpTel.Text.Trim()
.Parameters.Add("@start_emp", SqlDbType.DateTime).Value = dtpWork.Value.Date
.Parameters.Add("@Picture", SqlDbType.Image).Value = CurrentImage
Dim result As Integer
result = .ExecuteNonQuery()
'กรณีมีการบันทึกรูปภาพ
If tmpFileName <> "" Then
My.Computer.FileSystem.DeleteFile(tmpFileName)
End If
CurrentImage = Nothing
If result = 0 Then
MessageBox.Show("รหัสที่คุณป้อน ไม่พบในฐานข้อมูล!!! ", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtEmpId.SelectAll()
Else
MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว!!! !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End With
Catch ex As Exception
MessageBox.Show("เกิดข้อผิดพลาดเนื่องจาก" & ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
IsChangeImage = False
ShowAllEmployee()
clear()
End Sub
'การลบข้อมูลพนักงาน
Private Sub cmdDel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDel1.Click
If MessageBox.Show("คุณต้องการลบข้อมูลพนักงานใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
Exit Sub
End If
Dim sqlDelete As String
sqlDelete = "delete from Employee "
sqlDelete &= "Where emp_id = @emp_id"
Dim myCom As New SqlCommand
Dim rDelete As Integer
With (myCom)
.Parameters.Add("@emp_id", SqlDbType.NVarChar).Value = txtEmpId.Text.Trim()
.CommandText = sqlDelete
.CommandType = CommandType.Text
.Connection = Conn
rDelete = .ExecuteNonQuery()
End With
If rDelete > 0 Then
MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว")
Else
MessageBox.Show("ไม่พบที่ต้องการลบ")
End If
ShowAllEmployee()
clear()
End Sub
Private Sub cmdClear1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear1.Click
clear()
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picPicture.Click
With ofDlg
.Title = "กรุณาเลือกรูปภาพ"
.Filter = "JPEG(*.jpg)|*.jpg|Bitmap (*.bmp)|*.bmp|GIF(*.gif)|*.gif"
.FileName = ""
.Multiselect = False
.FilterIndex = 0
.ShowDialog()
ImageFileName = .FileName
If .FileName <> "" Then
picPicture.Image = Image.FromFile(ImageFileName)
Else
ImageFileName = ""
picPicture.Image = picPicture.ErrorImage
End If
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ImageFileName = ""
picPicture.Image = picPicture.ErrorImage
End Sub
End Class