-ขออนุญาตมาตั้งหัวข้อใหม่เพราะตอนนี้ปัญหามันเปลี่ยนไป
-ในUpdate ได้เปลี่ยนจาก Where His_product เป็น Where ID ซึ่งในฐานข้อมูลเป็นAutonumberและเป็นคีย์หลัก
อันนี้เป็นโค้ดของปุ่มUpdate
Code (VB.NET)
Try
Dim cmd As New OleDbCommand
If Not con.State = ConnectionState.Open Then
'open connection
con.Open()
End If
cmd.Connection = con
'check whether add new or update
If Me.semat.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = " INSERT INTO Tb_History(His_product, His_number, His_student, His_teacher, His_date, His_time)" & _
" VALUES ('" & Me.semat.Text & "'," & Me.txtnum.Text & ",'" & _
Me.txtna1.Text & "','" & Me.txtna2.Text & "','" & Me.txtdate.Text & "','" & _
Me.txttime.Text & "')"
cmd.ExecuteNonQuery()
Else
'update data in table
Dim intsi As Integer
cmd.CommandText = " Update Tb_History SET " & _
" His_product= '" & Me.semat.Text & "' " & _
" ,His_number=" & Me.txtnum.Text & _
" ,His_student= '" & Me.txtna1.Text & "' " & _
" ,His_teacher= '" & Me.txtna2.Text & "' " & _
" ,His_date= '" & Me.txtdate.Text & "' " & _
" ,His_time= '" & Me.txttime.Text & "' " & _
" WHERE ID= " & intsi & ""
cmd.ExecuteNonQuery() 'ใช้กับ Insert update delete
End If
'refresh data in list
RefreshData()
'close connection
con.Close()
MessageBox.Show("บันทึกลงตารางเรียบร้อยแล้ว")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
End Try
และอันนี้เป็นโค้ดของปุ่มEdit Code (VB.NET)
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intsi As Integer = Me.DataGridView1.SelectedRows(0).Cells("ID").Value
Dim intsis As String = Me.DataGridView1.SelectedRows(0).Cells("His_product").Value
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'get data into datatable
Dim da As New OleDbDataAdapter("SELECT * FROM Tb_History " & _
" WHERE ID = " & intsi & "", con)
Dim dt As New DataTable()
da.Fill(dt)
Me.semat.Text = dt.Rows(0).Item("His_product")
Me.txtnum.Text = dt.Rows(0).Item("His_number")
Me.txtna1.Text = dt.Rows(0).Item("His_student")
Me.txtna2.Text = dt.Rows(0).Item("His_teacher")
Me.txtdate.Text = dt.Rows(0).Item("His_date")
Me.txttime.Text = dt.Rows(0).Item("His_time")
'hide the Hisproduct to be edited in TAG of semat.text in case His_product changed
Me.semat.Tag = intsi 'หรือป่าว
'change button add to อัพเดทข้อมูล
Me.add.Text = "อัพเดทข้อมูล"
Me.RefreshData()
'disable button edit
Me.edit.Enabled = True
'close connection
con.Close()
End If
End If
End If
End Sub
Dim cmd As New OleDbCommand
If Not con.State = ConnectionState.Open Then
'open connection
con.Open()
End If
cmd.Connection = con
'check whether add new or update
If Me.semat.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = " INSERT INTO Tb_History(His_product, His_number, His_student, His_teacher, His_date, His_time)" & _
" VALUES ('" & Me.semat.Text & "'," & Me.txtnum.Text & ",'" & _
Me.txtna1.Text & "','" & Me.txtna2.Text & "','" & Me.txtdate.Text & "','" & _
Me.txttime.Text & "')"
cmd.ExecuteNonQuery()
Else
'update data in table
Dim srtid As Integer
cmd.CommandText = "Update Tb_History " & _
" SET ID =" & srtid & "" & _
" ,His_product= '" & Me.semat.Text & "' " & _
" ,His_number=" & Me.txtnum.Text & _
" ,His_student= '" & Me.txtna1.Text & "' " & _
" ,His_teacher= '" & Me.txtna2.Text & "' " & _
" ,His_date= '" & Me.txtdate.Text & "' " & _
" ,His_time= '" & Me.txttime.Text & "' " & _
" Where ID = " & srtid & " "
cmd.ExecuteNonQuery() 'ใช้กับ Insert update delete
End If
'refresh data in list
RefreshData()
'close connection
con.Close()
'clear form เพิ่มมา
Me.cancel.PerformClick()
MessageBox.Show("บันทึกลงตารางเรียบร้อยแล้ว")
ปุ่มแก้ไข Code (VB.NET)
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลบรรทัดนั้ใช่หรือไม่ ?", "ยืนยันการแก้ไขข้อมูล", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intsi As Integer = Me.DataGridView1.SelectedRows(0).Cells("ID").Value
'open connnection
If Not con.State = ConnectionState.Open Then
con.Open()
End If
'get data into datatable
Dim strSQL As String = "SELECT * FROM Tb_History " & _
" WHERE ID = " & intsi & " "
Dim da As New OleDbDataAdapter(strSQL, con)
Dim dt As New DataTable()
da.Fill(dt)
Me.semat.Text = dt.Rows(0)("His_product")
Me.txtnum.Text = dt.Rows(0)("His_number")
Me.txtna1.Text = dt.Rows(0)("His_student")
Me.txtna2.Text = dt.Rows(0)("His_teacher")
Me.txtdate.Text = dt.Rows(0)("His_date")
Me.txttime.Text = dt.Rows(0)("His_time")
'hide the Hisproduct to be edited in TAG of semat.text in case His_product changed
'ถ้าเปลี่ยนเป็นstr มันจะกลายเป็นเพิ่มแถวใหม่พอกดอัพเดท และเอาออกก็เปลี่ยนการเพิ่มแถว
Me.semat.Tag = intsi
'change button add to อัพเดทข้อมูล
Me.add.Text = "อัพเดทข้อมูล"
Me.RefreshData()
'disable button edit
Me.edit.Enabled = False
'close connection
con.Close()
End If
End If
End If