Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class Datasut
Private bs As New BindingSource()
Dim cnn As New OleDb.OleDbConnection
Private Sub TSBExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBExit.Click
If MsgBox("คุณต้องการออกจากโปรแกรมใช่หรือไม่", vbYesNo + vbQuestion, "ยืนยัน") = vbYes Then
End
End If
End Sub
Private Sub DatasueitBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DatasueitBindingNavigatorSaveItem.Click
Me.Validate()
Me.DatasueitBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DBMSDataSet)
End Sub
Private Sub Datasut_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "Provider=Microsoft.ace.Oledb.12.0;" & "Data Source=DBMS.accdb"
'
'get data into list
Me.RefreshData()
End Sub
Private Sub RefreshData()
If cnn.State = ConnectionState.Closed Then
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Datasueit ORDER BY ID_sut ", cnn)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
Me.dgvsut.DataSource = dt
End Sub
Private Sub TSBSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBSave.Click
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
Dim sql As String
Dim cmd As OleDb.OleDbCommand
Dim IngRows As Integer
'check whether save new or update
If Me.txtID_sut.Tag & " " = " " Then
' add new
'add data to table
sql = "INSERT INTO" & _
"Datasueit(ID_sut, " & _
"Kind_sut,Colour_sut,Cost_sut, " & _
"Rent_sut,Pled_sut,Num_sut,Point_sut, " & _
"Origin_sut,Status_sut,Detail_sut,Pic_sut)" & _
"VALUES (@ID,@Kind,@Colour,@Cost,@Rent,@Pled,@Num, " & _
"@Point,@Origin,@Status,@Detail,@Pic)"
cmd = New OleDb.OleDbCommand(sql, cnn)
cmd.Parameters.AddWithValue("@ID", txtID_sut.Text)
cmd.Parameters.AddWithValue("@Kind", CmbKind_sut.Text)
cmd.Parameters.AddWithValue("@Colour", CmbColour_sut.Text)
cmd.Parameters.AddWithValue("@Cost", txtCost_sut.Text)
cmd.Parameters.AddWithValue("@Rent", txtRent_sut.Text)
cmd.Parameters.AddWithValue("@Pled", txtPled_sut.Text)
cmd.Parameters.AddWithValue("@Num", txtNum_sut.Text)
cmd.Parameters.AddWithValue("@Point", txtPoint_sut.Text)
cmd.Parameters.AddWithValue("@Origin", txtOrigin_sut.Text)
cmd.Parameters.AddWithValue("@Status", cmbStatus_sut.Text)
cmd.Parameters.AddWithValue("@Detail", txtDetail_sut.Text)
cmd.Parameters.AddWithValue("@Pic", Pic_sut.Image)
If (cnn.State = ConnectionState.Closed) Then
cnn.Open()
IngRows = cmd.ExecuteNonQuery()
MsgBox(IngRows & "บันทึกข้อมูลเรียบร้อยแล้ว")
End If
' Try
' cmd.ExecuteNonQuery()
' Catch ex As Exception
' System.Windows.Forms.MessageBox.Show(ex.Message)
' End Try
cnn.Close()
Pic_sut.Image = Nothing
End If
'update data tin table
'sql = "UPDATE Datasueit SET ID_sut = @ID,Kind_sut=@Kind, " & _
' "Colour_sut=@Coulour,Cost_sut=@Cost,Rent_sut=@Rent," & _
' "Pled_sut=@Pled,Num_sut=@Num,Point_sut=@Point," & _
' "Origin_sut=@Origin,Status_sut=@Status,Detail_sut=@Detail,Pic_sut=@Pic"
''"WHERE ID_sut = @ID"
'cmd = New OleDb.OleDbCommand(sql, cnn)
'cmd.Parameters.Clear()
'cmd.CommandText = sql
'cmd.Parameters.AddWithValue("@ID", txtID_sut.Text)
'cmd.Parameters.AddWithValue("@Kind", CmbKind_sut.Text)
'cmd.Parameters.AddWithValue("@Colour", CmbColour_sut.Text)
'cmd.Parameters.AddWithValue("@Cost", txtCost_sut.Text)
'cmd.Parameters.AddWithValue("@Rent", txtRent_sut.Text)
'cmd.Parameters.AddWithValue("@Pled", txtPled_sut.Text)
'cmd.Parameters.AddWithValue("@Num", txtNum_sut.Text)
'cmd.Parameters.AddWithValue("@Point", txtPoint_sut.Text)
'cmd.Parameters.AddWithValue("@Origin", txtOrigin_sut.Text)
'cmd.Parameters.AddWithValue("@Status", cmbStatus_sut.Text)
'cmd.Parameters.AddWithValue("@Detail", txtDetail_sut.Text)
'cmd.Parameters.AddWithValue("@Pic", Pic_sut.Image)
'Try
' cmd.ExecuteNonQuery()
'Catch ex As Exception
' System.Windows.Forms.MessageBox.Show(ex.Message)
'End Try
'cnn.Close()
'End If
'refresh data in list
RefreshData()
'clear form
Me.TSBAdd.PerformClick()
'close connection
cnn.Close()
Me.TSBDelete.Enabled = False
Me.TSBSave.Enabled = False
Me.BtnChang.Enabled = False
End Sub
Private Sub TSBAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBAdd.Click
Me.txtID_sut.Text = " "
Me.txtCost_sut.Text = " "
Me.txtDetail_sut.Text = " "
Me.txtNum_sut.Text = " "
Me.txtOrigin_sut.Text = " "
Me.txtPled_sut.Text = " "
Me.txtPoint_sut.Text = " "
Me.txtRent_sut.Text = " "
Me.txtID_sut.Tag = " "
Me.TSBDelete.Enabled = True
Me.TSBSave.Enabled = True
Me.BtnChang.Enabled = True
Me.Pic_sut.Image = Nothing
Me.txtID_sut.Focus()
End Sub
Private Sub TSBEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBEdit.Click
'check for the selected item in list
If Me.dgvsut.Rows.Count > 0 Then
If Me.dgvsut.SelectedRows.Count > 0 Then
Dim charID_sut As Char = Me.dgvsut.SelectedRows(0).Cells("ID_sut").Value
'get data from database followed by student ID
'open connection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
'get data into datatable
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Datasueit " & _
"WHERE ID_sut " & charID_sut, cnn)
Dim dt As New DataTable
da.Fill(dt)
Me.txtID_sut.Text = charID_sut
Me.CmbKind_sut.Text = dt.Rows(0).Item("Kind")
Me.CmbColour_sut.Text = dt.Rows(0).Item("Colour")
Me.txtCost_sut.Text = dt.Rows(0).Item("Cost")
Me.txtNum_sut.Text = dt.Rows(0).Item("Num")
Me.txtOrigin_sut.Text = dt.Rows(0).Item("Origin")
Me.txtPled_sut.Text = dt.Rows(0).Item("Pled")
Me.txtPoint_sut.Text = dt.Rows(0).Item("Point")
Me.txtRent_sut.Text = dt.Rows(0).Item("Rent")
Me.cmbStatus_sut.Text = dt.Rows(0).Item("Status")
Me.txtDetail_sut.Text = dt.Rows(0).Item("Detail")
'hid the id to be edited in TAG of txtID_sut in case id is changed
Me.txtID_sut.Tag = charID_sut
'change button add to update
Me.TSBEdit.Enabled = False
'close connection
cnn.Close()
End If
End If
Me.TSBDelete.Enabled = True
Me.TSBSave.Enabled = True
End Sub
Private Sub TSBDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBDelete.Click
If MsgBox("คุณต้องการลบข้อมูลหรือไม่", vbYesNo + vbQuestion, "ยืนยัน") = vbYes Then
'check for the selected item in list
If Me.dgvsut.Rows.Count > 0 Then
If Me.dgvsut.SelectedRows.Count > 0 Then
Dim charID_sut As Char = Me.dgvsut.SelectedRows(0).Cells("ID_sut").Value
'open connection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
'delete data
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = cnn
cmd.CommandText = "DELETE FROM Datasueit WHERE ID_sut=" & charID_sut
cmd.ExecuteNonQuery()
'refresh data
Me.RefreshData()
'close connection
cnn.Close()
txtCost_sut.Text = " "
txtDetail_sut.Text = " "
txtfile.Text = " "
txtID_sut.Text = " "
txtNum_sut.Text = " "
txtOrigin_sut.Text = " "
txtPled_sut.Text = " "
txtPoint_sut.Text = " "
txtRent_sut.Text = " "
CmbColour_sut.SelectedValue = 0
CmbKind_sut.SelectedValue = 0
cmbStatus_sut.SelectedValue = 0
Pic_sut.Image = Nothing
MsgBox("ลบข้อมูลเรียบร้อยแล้ว", vbOKOnly, "ลบข้อมูล")
End If
End If
End If
Me.TSBDelete.Enabled = True
End Sub
Private Sub BtnChang_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnChang.Click
OpenFileDialog1.Filter = "Image File (*.jpg,*.png,*.gif,*.bmp)|*.jpg;*.png;*.gif;*.bmp"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Pic_sut.Image = Bitmap.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
bs.MoveNext()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
bs.MovePrevious()
End Sub
Private Sub TSBSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBSearch.Click
Try
Me.DatasueitTableAdapter.ID_sut(Me.DBMSDataSet.Datasueit, txtSearch.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
End Class