Imports System.Data.SqlClient
Public Class frmmember
Dim cn As New SqlConnection("Server=localhost;Initial Catalog=Cookies;Integrated Security=SSPI")
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim dt As New DataTable
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Short
cn.Open()
Using cmd As New SqlClient.SqlCommand("INSERT INTO Member(Mem_name,Mem_last,Mem_birth,Mem_add,Mem_email,Mem_tel) values('" & TextBox2.Text & "','" & TextBox3.Text & "','" & Format(DateTimePicker1.Value, "MM/dd/yyyy") & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "')", cn)
i = cmd.ExecuteNonQuery()
End Using
cn.Close()
If (i > 0) Then
MessageBox.Show("Success Save Data")
showdata()
End If
End Sub
Private Sub showdata()
DataGridView1.RowTemplate.Height = 50
cn.Open()
With cmd
.Connection = cn
.CommandText = "select * from Member"
End With
da.SelectCommand = cmd
dt.Clear()
da.Fill(dt)
DataGridView1.DataSource = dt
cn.Close()
DataGridView1.Columns(4).Width = 130
For i As Integer = 0 To DataGridView1.Columns.Count - 1
If TypeOf DataGridView1.Columns(i) Is DataGridViewImageColumn Then
DirectCast(DataGridView1.Columns(i), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
End If
Next
cn.Close()
End Sub
Private Sub frmmember_Load(sender As Object, e As EventArgs) Handles MyBase.Load
showdata()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Me.Hide()
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
TextBox2.Text = DataGridView1.Item(1, e.RowIndex).Value
TextBox3.Text = DataGridView1.Item(2, e.RowIndex).Value
DateTimePicker1.Text = DataGridView1.Item(3, e.RowIndex).Value
TextBox4.Text = DataGridView1.Item(4, e.RowIndex).Value
TextBox5.Text = DataGridView1.Item(5, e.RowIndex).Value
TextBox6.Text = DataGridView1.Item(6, e.RowIndex).Value
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim i As Short
cn.Open()
Using cmd As New SqlClient.SqlCommand("Update Member set Mem_name = '" & TextBox2.Text & "',Mem_last = '" & TextBox3.Text & "', Mem_birth'" & DateTimePicker1.Text & "',Mem_add = '" & TextBox4.Text & "', Mem_email = '" & TextBox5.Text & "',Mem_tel = '" & TextBox6.Text & "' where Mem_id = " & DataGridView1.SelectedRows(0).Cells(0).Value & "", cn)
i = cmd.ExecuteNonQuery()
End Using
cn.Close()
If (i > 0) Then
MessageBox.Show("Update Data Success")
showdata()
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim i As Short
cn.Open()
With cmd
.Connection = cn
.CommandText = "delete from Member Where Mem_id = " & DataGridView1.SelectedRows(0).Cells(0).Value
i = .ExecuteNonQuery()
End With
If (i > 0) Then
MsgBox("Delete Success")
End If
cn.Close()
showdata()
End Sub
End Class