Public Class FrmCustomer
Private Sub FormCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If con.State = ConnectionState.Closed Then
con.Open()
End If
loadfile()
con.Close()
End Sub
Private Sub BtnCanel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCanel.Click
txtIdCus.Text = ""
txtNameCus.Text = ""
txtSexCus.Text = ""
txtAddressCus.Text = ""
txtTelCus.Text = ""
End Sub
Private Sub addCus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddCus.Click
Dim Query As String
Query = "INSERT INTO customer"
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=tree;User ID=root;Password=123456;")
Dim sql As MySqlCommand = New MySqlCommand(Query, con)
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
'lblMsg.Text = "Record is Successfully Inserted"
'Else
'lblMsg.Text = "Record is not Inserted"
'End If
con.Close()
End Sub
Private Sub BtnEditCus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEditCus.Click
Dim Query As String
con.Open()
Query = "UPDATE Customer SET CustomerName ='" + txtNameCus.Text + "'"
Query = Query + ",Sex = '" + txtSexCus.Text + "'"
Query = Query + ",Address = '" + txtAddressCus.Text + "'"
Query = Query + ",Telephone = '" + txtTelCus.Text + "'"
Query = Query + " WHERE CustomerID = '" + txtIdCus.Text + "'"
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
MsgBox(Query)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
'lblMsg.Text = "Record is Successfully Updated"
' lblMsg.Text = "Record is not Updated"
'End If
con.Close()
End Sub
Private Sub BtndelCus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtndelCus.Click
Dim Query As String
con.Open()
Dim xx As Integer
xx = DataGridView1.CurrentRow.Index
'MsgBox(DataGridView1.Item(0, xx).Value)
Dim zz As String
zz = DataGridView1.Item(0, xx).Value.ToString
Query = "Delete FROM customer WHERE CustomerId =" + zz
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
MsgBox(Query)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
' lblMsg.Text = "Record is Successfully Deleted"
'Else
' lblMsg.Text = "Record is not Deleted"
'End If
con.Close()
End Sub
Public Sub loadfile()
Dim sqlQuery As String = "select * from customer"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCmd As New MySqlCommand
Dim sqlTable As New DataTable
Dim i As Integer
With sqlCmd
.CommandText = sqlQuery
.Connection = con
End With
With sqlAdapter
.SelectCommand = sqlCmd
.Fill(sqlTable)
End With
For i = 0 To sqlTable.Rows.Count - 1
DataGridView1.Rows.Add(sqlTable.Rows(i)("CustomerID"), (sqlTable.Rows(i)("CustomerName")), (sqlTable.Rows(i)("Sex")),
(sqlTable.Rows(i)("Address")), (sqlTable.Rows(i)("Telephone")))
Next
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
txtIdCus.Text = DataGridView1.Item(0, i).Value
txtNameCus.Text = DataGridView1.Item(1, i).Value
txtSexCus.Text = DataGridView1.Item(2, i).Value
txtAddressCus.Text = DataGridView1.Item(3, i).Value
txtTelCus.Text = DataGridView1.Item(4, i).Value
End Sub
Private Sub BtnSeachCus_Click(sender As System.Object, e As System.EventArgs) Handles BtnSeachCus.Click
Dim sqlQuery As String = "select * from customer WHERE CustomerID = "
sqlQuery += txtIdCus.Text()
MsgBox(sqlQuery)
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCmd As New MySqlCommand
Dim sqlTable As New DataTable
Dim i As Integer
With sqlCmd
.CommandText = sqlQuery
.Connection = con
End With
With sqlAdapter
.SelectCommand = sqlCmd
.Fill(sqlTable)
End With
For i = 0 To sqlTable.Rows.Count - 1
DataGridView1.Rows.Add(sqlTable.Rows(i)("CustomerID"), (sqlTable.Rows(i)("CustomerName")), (sqlTable.Rows(i)("Sex")),
(sqlTable.Rows(i)("Address")), (sqlTable.Rows(i)("Telephone")))
Next
End Sub