Private Sub FrmTeacher_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Sql = "SELECT t_teacher.*, t_title_name.title_name FROM t_teacher INNER JOIN t_title_name ON t_teacher.title_id = t_title_name.title_id ORDER BY tc_id ASC"
Da_TeaCher.MissingSchemaAction = MissingSchemaAction.AddWithKey
Da_TeaCher.SelectCommand = New OleDbCommand(Sql, Con)
Try
Da_TeaCher.Fill(Ds_Teacher, "show_teacher")
Grid_Teacher.DataSource = Ds_Teacher.Tables("show_teacher")
Obj_Teacher = Me.BindingContext(Ds_Teacher.Tables("show_teacher"))
Sql = "SELECT * FROM t_title_name WHERE title_id > '02'"
Da_TitleName.MissingSchemaAction = MissingSchemaAction.AddWithKey
Da_TitleName.SelectCommand = New OleDbCommand(Sql, Con)
Try
Da_TitleName.Fill(Ds_TitleName, "title")
ComboTitle.DataSource = Ds_TitleName.Tables("title")
ComboTitle.DisplayMember = "title_name"
ComboTitle.ValueMember = "title_id"
Call Show_Data(Obj_Teacher.Position)
Call BtnStatus(False)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Code (VB.NET)
Dim Dr_Save As DataRow = Nothing
Dim Cb As New OleDbCommandBuilder(Da_TeaCher)
If Btn = "add" Then
Dr_Save = Ds_Teacher.Tables("show_teacher").NewRow
With Dr_Save
.BeginEdit()
.Item("tc_id") = TxtId.Text
.Item("title_id") = ComboTitle.SelectedValue
.Item("tc_fname") = TxtFname.Text
.Item("tc_lname") = TxtLname.Text
.EndEdit()
End With
Ds_Teacher.Tables("show_teacher").Rows.Add(Dr_Save)
Da_TeaCher.InsertCommand = Cb.GetInsertCommand
ElseIf Btn = "edit" Then
Dr_Save = Ds_Teacher.Tables("show_teacher").Rows(Obj_Teacher.Position)
With Dr_Save
.BeginEdit()
.Item("title_id") = ComboTitle.SelectedValue
.Item("tc_fname") = TxtFname.Text
.Item("tc_lname") = TxtLname.Text
.EndEdit()
End With
Da_TeaCher.UpdateCommand = Cb.GetUpdateCommand 'ERROR Dynamic SQL generation is not supported against multiple base tables.
End If
Da_TeaCher.Update(Ds_Teacher.Tables("show_teacher"))
Ds_Teacher.AcceptChanges()
Call Clear_Ds()
If Btn = "add" Then
MessageBox.Show("การเพิ่มข้อมูลอาจารย์สมบูรณ์", "แจ้งให้ทราบ", MessageBoxButtons.OK, MessageBoxIcon.None)
Obj_Teacher.Position = Obj_Teacher.Count - 1
ElseIf Btn = "edit" Then
MessageBox.Show("การแก้ไขข้อมูลอาจารย์สมบูรณ์", "แจ้งให้ทราบ", MessageBoxButtons.OK, MessageBoxIcon.None)
End If
Call BtnStatus(False)
Call Show_Data(Obj_Teacher.Position)