รบกวนดู Code ให้หน่อยค่ะคือ ในส่วน Combpbox มาเป็น Name แต่ตอนบันทึกต้องการเป็น ID
Code (PHP)
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Text
Public Class frm_AddEmp
Dim cn As New SqlConnection("Data Source=HQ-IT-IT03;Initial Catalog=parameterproject; Integrated Security=SSPI;")
'Dim cmd As New SqlCommand
'Dim DA As New SqlDataAdapter
'Dim Sql As String
'Dim Ds As DataSet
'Dim DR As SqlDataReader
'Dim DT As New DataTable
Dim sb As StringBuilder
Private Sub frm_AddEmp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
get_autonumber()
refresh_AddEmp()
change_datagrid_column()
Sql = "select fmName from tbl_MacForming"
cmd_database_to_object(txt_fmID)
Combobox()
End Sub
Private Sub get_autonumber()
Dim id As String
Sql = "SELECT Convert(INT, MAX(RIGHT(EmpNumber, 5)))" &
" FROM EmpTable"
Try
'txt_MacNumber.Text = "F" & String.Format("{0:00000}", cmd_excuteScalar() + 1)
id = String.Format("{0:00000}", cmd_excuteScalar() + 1)
txt_EmpNumber.Text = "E" & id
Catch ex As Exception
txt_EmpNumber.Text = "00001"
End Try
End Sub
Private Sub refresh_AddEmp()
Sql = "select * from EmpTable"
datagrid_data.DataSource = cmd_excuteDataTable()
End Sub
Private Sub Combobox()
Dim sb As StringBuilder
Dim Sql = "SELECT * FROM tbl_MacForming"
Sql = sb.ToString()
cmd = New SqlCommand()
cmd.CommandText = Sql
cmd.CommandType = CommandType.Text
cmd.Connection = cn
DR = cmd.ExecuteReader()
If (DR.HasRows) Then
DT = New DataTable()
DT.Load(DR)
txt_fmID.BeginUpdate()
txt_fmID.BindingContext = New BindingContext()
txt_fmID.DisplayMember = "fmName"
txt_fmID.ValueMember = "fmID"
txt_fmID.DataSource = DT
txt_fmID.EndUpdate()
End If
End Sub
Public Sub change_datagrid_column()
With datagrid_data
If .RowCount > 0 Then
.Columns(0).HeaderText = "EmpNumber"
.Columns(1).HeaderText = "ID"
.Columns(2).HeaderText = "ชื่อพนักงานประจำเครื่อง"
.Columns(3).HeaderText = "ชื่อครื่อง Forming"
'.Columns(0).Width = 20
'.Columns(1).Width = 20
'.Columns(2).Width = 20
'.Columns(3).Width = 20
End If
End With
End Sub
Private Sub btn_addnew_Click(sender As Object, e As EventArgs) Handles btn_addnew.Click
Sql = "insert into EmpTable (EmpNumber, EmpID, EmpName, fmID) values ('" & txt_EmpNumber.Text & "', '" & txt_EmpID.Text & "','" & txt_EmpName.Text & "','" & txt_fmID.ValueMember & "')"
If cmd_excuteNonquery() >= 1 Then
msg_ok("เพิ่มข้อมูลสำเร็จ")
refresh_AddEmp()
get_autonumber()
txt_EmpID.Text = ""
txt_EmpName.Text = ""
txt_fmID.Text = ""
End If
Combobox()
End Sub
Private Sub datagrid_data_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles datagrid_data.CellClick
Dim select_row As Integer = datagrid_data.CurrentRow.Index
Dim EmpNumber As String = datagrid_data.Item(0, select_row).Value
Sql = "select * from EmpTable where EmpNumber = '" & EmpNumber & "'"
Dim data_keep As DataTable = cmd_excuteDataTable()
txt_EmpNumber.Text = data_keep.Rows(0)("EmpNumber")
txt_EmpID.Text = data_keep.Rows(0)("EmpID")
txt_EmpName.Text = data_keep.Rows(0)("EmpName")
txt_fmID.Text = data_keep.Rows(0)("fmID")
End Sub
Private Sub btn_clear_Click(sender As Object, e As EventArgs) Handles btn_clear.Click
'txt_MacNumber.Text = ""
get_autonumber()
txt_EmpID.Text = ""
txt_EmpName.Text = ""
txt_fmID.Text = ""
End Sub
Private Sub btn_edit_Click(sender As Object, e As EventArgs) Handles btn_edit.Click
If confirm("คุณต้องการแก้ไขข้อมูลหรือไม่") = vbNo Then Return
Sql = "update EmpTable set EmpID = '" & txt_EmpID.Text & "', EmpName = '" & txt_EmpName.Text & "' where EmpNumber = '" & txt_EmpNumber.Text & "'"
'Sql = "update tbl_MacForming set fmID='AAA', fmName='BBB333' where fmNumber='F002'"
cmd = New SqlClient.SqlCommand(Sql, cn)
cmd.Parameters.Clear()
'cmd.Parameters.AddWithValue("ID", txt_fmID.Text)
'cmd.Parameters.AddWithValue("fmName", txt_fmName.Text)
'cmd.Parameters.AddWithValue("fNumber", txt_MacNumber.Text)
If cmd_excuteNonquery() >= 1 Then
msg_ok("แก้ไขสำเร็จ")
' 'txt_fmID.Text = ""
' 'txt_fmName.Text = ""
' 'get_autonumber()
refresh_AddEmp()
Else
msg_ok("ไม่สามารถแก้ไขได้")
End If
End Sub
Private Sub btn_del_Click(sender As Object, e As EventArgs) Handles btn_del.Click
Dim select_EmpID As Integer = datagrid_data.CurrentRow.Index
Dim EmpID As String = datagrid_data.Item(0, select_EmpID).Value
Sql = "delete from EmpTable where EmpNumber='" & txt_EmpNumber.Text & "'"
If cmd_excuteNonquery() = 0 Then
msg_error("ไม่สามารถลบข้อมูลได้")
Else
msg_ok("ลบข้อมูลสำเร็จ")
txt_EmpNumber.Text = ""
txt_EmpID.Text = ""
txt_EmpName.Text = ""
refresh_AddEmp()
get_autonumber()
End If
End Sub
Private Sub txt_search_TextChanged(sender As Object, e As EventArgs) Handles txt_search.TextChanged
Sql = "Select EmpNumber, EmpID, EmpName, fmID from EmpTable where EmpID Like '%" & txt_search.Text & "%' or EmpName like '%" & txt_search.Text & "%' or fmID like '%" & txt_search.Text & "%'"
datagrid_data.DataSource = cmd_excuteDataTable()
change_datagrid_column()
End Sub
Private Sub txt_EmpID_KeyDown(sender As Object, e As KeyEventArgs) Handles txt_EmpID.KeyDown
If e.KeyCode = Keys.Enter Then
txt_EmpName.Focus()
End If
End Sub
Private Sub txt_EmpName_KeyDown(sender As Object, e As KeyEventArgs) Handles txt_EmpName.KeyDown
If e.KeyCode = Keys.Enter Then
txt_fmID.Focus()
End If
End Sub
End Class