Imports System.Data.SqlClient
Public Class frmMember
Dim conn As SqlConnection
Dim status As Integer
Dim myadapter As SqlDataAdapter
Dim mydata As SqlDataReader
Dim sqlquery As String
Dim mycommand As SqlCommand
Dim dr As SqlDataReader
Dim dt As New DataTable
Dim com As SqlCommand
Dim da = New SqlDataAdapter
Private Sub frmMember_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New SqlConnection()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = Database.strConn.ToString
.Open()
End With
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
ComboBox1.Enabled = False
ComboBox2.Enabled = False
ComboBox3.Enabled = False
Button2.Enabled = False
Button4.Enabled = False
Button3.Enabled = True
Button6.Enabled = False
Button5.Enabled = False
filllistview()
End Sub
Public Sub filllistview()
mycommand = New SqlCommand()
sqlquery = "SELECT * FROM member"
mycommand.CommandText = sqlquery
mycommand.Connection = conn
mydata = mycommand.ExecuteReader
ListView1.Items.Clear()
While (mydata.Read())
With ListView1.Items.Add(mydata("m_id"))
.subitems.add(mydata("m_sex"))
.subitems.add(mydata("m_name"))
.subitems.add(mydata("m_class"))
.subitems.add(mydata("m_nhun"))
.subitems.add(mydata("m_phun"))
.subitems.add(mydata("m_thun"))
End With
End While
mycommand.Dispose()
mydata.Close()
conn.Close()
End Sub
Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
TextBox1.Text = ListView1.SelectedItems(0).SubItems(0).Text
ComboBox1.Text = ListView1.SelectedItems(0).SubItems(1).Text
TextBox2.Text = ListView1.SelectedItems(0).SubItems(2).Text
ComboBox2.Text = ListView1.SelectedItems(0).SubItems(3).Text
ComboBox3.Text = ListView1.SelectedItems(0).SubItems(4).Text
TextBox4.Text = ListView1.SelectedItems(0).SubItems(5).Text
TextBox5.Text = ListView1.SelectedItems(0).SubItems(6).Text
Button6.Enabled = True
Button5.Enabled = True
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox4.Clear()
TextBox5.Clear()
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
TextBox1.Focus()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
status = 1
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox4.Enabled = True
TextBox5.Enabled = True
ComboBox1.Enabled = True
ComboBox2.Enabled = True
ComboBox3.Enabled = True
Button2.Enabled = True
Button4.Enabled = True
TextBox1.Focus()
conn.Open()
filllistview()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
status = 2
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox4.Enabled = True
TextBox5.Enabled = True
ComboBox1.Enabled = True
ComboBox2.Enabled = True
ComboBox3.Enabled = True
Button2.Enabled = True
Button4.Enabled = True
Button3.Enabled = False
Button5.Enabled = False
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
status = 3
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox4.Enabled = True
TextBox5.Enabled = True
ComboBox1.Enabled = True
ComboBox2.Enabled = True
ComboBox3.Enabled = True
Button2.Enabled = True
Button4.Enabled = True
Button6.Enabled = False
Button3.Enabled = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If status = 1 Then
conn.Open()
sqlquery = "INSERT INTO member(m_id ,m_sex ,m_name ,m_class ,m_nhun ,m_phun ,m_thun)VALUES ('" + TextBox1.Text + "','" + ComboBox1.Text + "','" + TextBox2.Text + "','" + ComboBox2.Text + "','" + ComboBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "');"
mycommand.CommandText = sqlquery
mycommand.Connection = conn
mycommand.ExecuteNonQuery()
'If mydata.HasRows Then
' MsgBox("NO")
'Else
MsgBox("สำเร็จ")
mycommand.Dispose()
status = 0
TextBox1.Clear()
TextBox2.Clear()
TextBox4.Clear()
TextBox5.Clear()
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
TextBox1.Focus()
Me.filllistview()
conn.Close()
End If
If status = 2 Then
conn.Open()
sqlquery = "UPDATE member SET m_id = '" & TextBox1.Text & "', m_sex = '" & ComboBox1.Text & "', m_name = '" & TextBox2.Text & "', m_class = '" & ComboBox2.Text & "', m_nhun = '" & ComboBox3.Text & "', m_phun = '" & TextBox4.Text & "', m_thun = '" & TextBox5.Text & "' WHERE m_id = '" & TextBox1.Text & "';"
mycommand.CommandText = sqlquery
mycommand.Connection = conn
mycommand.ExecuteNonQuery()
'mydata = mycommand.ExecuteReader
'mydata.Read()
MsgBox("สำเร็จ")
mycommand.Dispose()
TextBox1.Clear()
TextBox2.Clear()
TextBox4.Clear()
TextBox5.Clear()
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
TextBox1.Focus()
Me.filllistview()
conn.Close()
End If
If status = 3 Then
conn.Open()
sqlquery = "DELETE FROM member WHERE m_id = '" & TextBox1.Text & "';"
mycommand.CommandText = sqlquery
mycommand.Connection = conn
mycommand.ExecuteNonQuery()
'mydata = mycommand.ExecuteReader
'mydata.Read()
MsgBox("สำเร็จ")
mycommand.Dispose()
TextBox1.Clear()
TextBox2.Clear()
TextBox4.Clear()
TextBox5.Clear()
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
TextBox1.Focus()
Me.filllistview()
conn.Close()
End If
status = 0
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
ComboBox1.Enabled = False
ComboBox2.Enabled = False
ComboBox3.Enabled = False
Button2.Enabled = False
Button4.Enabled = False
Button3.Enabled = True
Button6.Enabled = False
Button5.Enabled = True
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
frmMain.Show()
Me.Close()
End Sub
Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
End Sub
Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
Try
Dim i As Integer = CInt(TextBox4.Text.Trim)
If (ComboBox3.Text <> "") Then
TextBox5.Text = i * CInt(ComboBox3.Text)
End If
Catch ex As Exception
TextBox4.Text = "10"
End Try
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
End Sub
Private Function PaddingConverter() As Integer
Throw New NotImplementedException
End Function
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
conn.Open()
Dim sql5 As String = "SELECT * FROM member Where m_id ='" & txtSearch.Text.Trim & "'"
com = New SqlCommand
With com
.CommandType = CommandType.Text
.CommandText = sql5
.Connection = conn
.Parameters.Clear()
.Parameters.Add("m_id", SqlDbType.VarChar).Value = txtSearch.Text.Trim
dr = .ExecuteReader()
End With
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
TextBox1.Text = dt.Rows(dt.Rows.Count - 1).Item("m_id").ToString
TextBox2.Text = dt.Rows(dt.Rows.Count - 1).Item("m_name").ToString
ComboBox1.Text = dt.Rows(dt.Rows.Count - 1).Item("m_sex").ToString
ComboBox2.Text = dt.Rows(dt.Rows.Count - 1).Item("m_class").ToString
ComboBox3.Text = dt.Rows(dt.Rows.Count - 1).Item("m_nhun").ToString
TextBox4.Text = dt.Rows(dt.Rows.Count - 1).Item("m_phun").ToString
TextBox5.Text = dt.Rows(dt.Rows.Count - 1).Item("m_thun").ToString
dt.Clear()
dr.Close()
conn.Close()
Else
MessageBox.Show("ไม่พบรหัสสมาชิกนี้", "การตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
dr.Close()
conn.Close()
End If
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
ComboBox1.Enabled = False
ComboBox2.Enabled = False
ComboBox3.Enabled = False
Button2.Enabled = False
Button4.Enabled = True
Button8.Enabled = True
Button6.Enabled = True
Button5.Enabled = True
End Sub
End Class