You are on page 1of 19

Quimbo, Prytj Elmo G.

IT6-6550

Activity 6_CRUD Operation


Display:

Database use PhpAdmin:


Table with values:

Source Code CRUD:

Imports System.Net.NetworkInformation
Imports System.Windows.Forms.VisualStyles
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports MySql.Data.MySqlClient
Imports Mysqlx
Imports Org.BouncyCastle.Pqc.Crypto

Public Class Form1


Dim conn As New MySqlConnection("server= localhost; port=3306; username=root;
password=; database=quimbo_vb")
Dim i As Integer
Dim dr As MySqlDataReader

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


DGV_load()
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles


btnSave.Click
Save()
DGV_load()
End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles


btnUpdate.Click
Update()
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles
btnDelete.Click
Delete()
End Sub

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles


btnSearch.Click
Dim SearchProductNo As Integer
If Integer.TryParse(tbSearch.Text, SearchProductNo) Then
Search(SearchProductNo)
Else
MessageBox.Show("Please enter a valid ProductNo (numeric).", "Invalid
ProductNo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub

Private Sub dgv_CellContentClick(sender As Object, e As


DataGridViewCellEventArgs) Handles dgv.CellContentClick
tbProdNo.Text = dgv.CurrentRow.Cells(0).Value
tbName.Text = dgv.CurrentRow.Cells(1).Value
tbPrice.Text = dgv.CurrentRow.Cells(2).Value
cbProdGroup.Text = dgv.CurrentRow.Cells(3).Value
dtpExDate.Value = dgv.CurrentRow.Cells(4).Value
cbAvailability.Checked = dgv.CurrentRow.Cells(5).Value

tbProdNo.ReadOnly = True
btnSave.Enabled = False
End Sub

Public Sub DGV_load()


dgv.Rows.Clear()

Try
conn.Open()
Dim cmd As New MySqlCommand("SELECT * FROM product", conn)
dr = cmd.ExecuteReader

While dr.Read
dgv.Rows.Add(dr.Item("ProductNumber"), dr.Item("ProductName"),
dr.Item("Price"), dr.Item("ProductGroup"), dr.Item("ExpirationDate"),
dr.Item("Availability"))
End While

Catch ex As Exception
MsgBox(ex.Message)
Finally
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End Try
End Sub

Public Sub clear()


tbProdNo.Clear()
tbName.Clear()
tbPrice.Clear()
cbProdGroup.Text = ""
dtpExDate.Value = Now
cbAvailability.CheckState = False
btnSave.Enabled = True
End Sub

Public Sub Save()


Try
conn.Open()
Dim cmd As New MySqlCommand("INSERT INTO `product`(`ProductNumber`,
`ProductName`, `Price`, `ProductGroup`, `ExpirationDate`, `Availability`) VALUES
(@ProductNumber,@ProductName,@Price,@ProductGroup,@ExpirationDate,@Availability)",
conn)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@ProductNumber", tbProdNo.Text)
cmd.Parameters.AddWithValue("@ProductName", tbName.Text)
cmd.Parameters.AddWithValue("@Price", CDec(tbPrice.Text))
cmd.Parameters.AddWithValue("@ProductGroup", cbProdGroup.Text)
cmd.Parameters.AddWithValue("@ExpirationDate", CDate(dtpExDate.Value))
cmd.Parameters.AddWithValue("@Availability", cbAvailability.Checked)

i = cmd.ExecuteNonQuery
If i > 0 Then
MessageBox.Show("Record Save Success!", "CRUD",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Record Save Failed!", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If

Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
clear()
End Sub

Sub Update()
Try
conn.Open()
Dim cmd As New MySqlCommand("UPDATE `product` SET
`ProductName`=@ProductName,`Price`=@Price,`ProductGroup`=@ProductGroup,`ExpirationDa
te`=@ExpirationDate,`Availability`=@Availability WHERE
`ProductNumber`=@ProductNumber", conn)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@ProductNumber", tbProdNo.Text)
cmd.Parameters.AddWithValue("@ProductName", tbName.Text)
cmd.Parameters.AddWithValue("@Price", CDec(tbPrice.Text))
cmd.Parameters.AddWithValue("@ProductGroup", cbProdGroup.Text)
cmd.Parameters.AddWithValue("@ExpirationDate", CDate(dtpExDate.Value))
cmd.Parameters.AddWithValue("@Availability", cbAvailability.Checked)

i = cmd.ExecuteNonQuery
If i > 0 Then
MessageBox.Show("Record Update Success!", "CRUD",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Record Update Failed!", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If

Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
clear()
DGV_load()
End Sub

Sub Delete()
Try
conn.Open()
Dim cmd As New MySqlCommand("DELETE FROM product WHERE
ProductNumber=@ProductNumber", conn)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@ProductNumber", tbProdNo.Text)
i = cmd.ExecuteNonQuery
If i > 0 Then
MessageBox.Show("Record Delete Success!", "CRUD",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Record Delete Failed!", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If

Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
clear()
DGV_load()
End Sub

Sub Search(SearchProductNo As Integer)


dgv.Rows.Clear()

Try
conn.Open()
Dim cmd As New MySqlCommand("SELECT * FROM product WHERE ProductNumber =
@ProductNumber", conn)
cmd.Parameters.AddWithValue("@ProductNumber", SearchProductNo)
dr = cmd.ExecuteReader

While dr.Read
dgv.Rows.Add(dr.Item("ProductNumber"), dr.Item("ProductName"),
dr.Item("Price"), dr.Item("ProductGroup"), dr.Item("ExpirationDate"),
dr.Item("Availability"))
End While

Catch ex As Exception
MsgBox(ex.Message)
Finally
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End Try
End Sub

Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles


btnRefresh.Click
DGV_load()
End Sub
End Class

Source Code Design:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.


<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer


'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
txtProdNo = New Label()
tbProdNo = New TextBox()
txtProdName = New Label()
tbName = New TextBox()
txtPrice = New Label()
tbPrice = New TextBox()
txtProdGroup = New Label()
cbProdGroup = New ComboBox()
txtExpiry = New Label()
dtpExDate = New DateTimePicker()
cbAvailability = New CheckBox()
dgv = New DataGridView()
Column1 = New DataGridViewTextBoxColumn()
Column2 = New DataGridViewTextBoxColumn()
Column3 = New DataGridViewTextBoxColumn()
Column4 = New DataGridViewTextBoxColumn()
Column5 = New DataGridViewTextBoxColumn()
Column6 = New DataGridViewCheckBoxColumn()
btnSave = New Button()
btnUpdate = New Button()
btnDelete = New Button()
btnClear = New Button()
tbSearch = New TextBox()
btnRefresh = New Button()
btnSearch = New Button()
CType(dgv, ComponentModel.ISupportInitialize).BeginInit()
SuspendLayout()
'
' txtProdNo
'
txtProdNo.AutoSize = True
txtProdNo.Location = New Point(74, 74)
txtProdNo.Name = "txtProdNo"
txtProdNo.Size = New Size(65, 15)
txtProdNo.TabIndex = 0
txtProdNo.Text = "ProductNo"
'
' tbProdNo
'
tbProdNo.Location = New Point(145, 71)
tbProdNo.Name = "tbProdNo"
tbProdNo.Size = New Size(160, 23)
tbProdNo.TabIndex = 1
'
' txtProdName
'
txtProdName.AutoSize = True
txtProdName.Location = New Point(58, 121)
txtProdName.Name = "txtProdName"
txtProdName.Size = New Size(81, 15)
txtProdName.TabIndex = 2
txtProdName.Text = "ProductName"
'
' tbName
'
tbName.Location = New Point(145, 118)
tbName.Name = "tbName"
tbName.Size = New Size(160, 23)
tbName.TabIndex = 3
'
' txtPrice
'
txtPrice.AutoSize = True
txtPrice.Location = New Point(106, 167)
txtPrice.Name = "txtPrice"
txtPrice.Size = New Size(33, 15)
txtPrice.TabIndex = 4
txtPrice.Text = "Price"
'
' tbPrice
'
tbPrice.Location = New Point(145, 164)
tbPrice.Name = "tbPrice"
tbPrice.Size = New Size(160, 23)
tbPrice.TabIndex = 5
'
' txtProdGroup
'
txtProdGroup.AutoSize = True
txtProdGroup.Location = New Point(57, 214)
txtProdGroup.Name = "txtProdGroup"
txtProdGroup.Size = New Size(82, 15)
txtProdGroup.TabIndex = 6
txtProdGroup.Text = "ProductGroup"
'
' cbProdGroup
'
cbProdGroup.FormattingEnabled = True
cbProdGroup.Items.AddRange(New Object() {"Shoes", "Tshirt"})
cbProdGroup.Location = New Point(145, 211)
cbProdGroup.Name = "cbProdGroup"
cbProdGroup.Size = New Size(160, 23)
cbProdGroup.TabIndex = 7
'
' txtExpiry
'
txtExpiry.AutoSize = True
txtExpiry.Location = New Point(52, 264)
txtExpiry.Name = "txtExpiry"
txtExpiry.Size = New Size(87, 15)
txtExpiry.TabIndex = 8
txtExpiry.Text = "Expiration Date"
'
' dtpExDate
'
dtpExDate.Location = New Point(145, 258)
dtpExDate.Name = "dtpExDate"
dtpExDate.Size = New Size(191, 23)
dtpExDate.TabIndex = 9
'
' cbAvailability
'
cbAvailability.AutoSize = True
cbAvailability.Location = New Point(145, 298)
cbAvailability.Name = "cbAvailability"
cbAvailability.Size = New Size(84, 19)
cbAvailability.TabIndex = 10
cbAvailability.Text = "Availability"
cbAvailability.UseVisualStyleBackColor = True
'
' dgv
'
dgv.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.AutoSize
dgv.Columns.AddRange(New DataGridViewColumn() {Column1, Column2,
Column3, Column4, Column5, Column6})
dgv.Location = New Point(343, 74)
dgv.Name = "dgv"
dgv.RowHeadersVisible = False
dgv.Size = New Size(603, 286)
dgv.TabIndex = 11
'
' Column1
'
Column1.HeaderText = "ProductNo."
Column1.Name = "Column1"
'
' Column2
'
Column2.HeaderText = "ProductName"
Column2.Name = "Column2"
'
' Column3
'
Column3.HeaderText = "Price"
Column3.Name = "Column3"
'
' Column4
'
Column4.HeaderText = "ProductGroup"
Column4.Name = "Column4"
'
' Column5
'
Column5.HeaderText = "Expiration Date"
Column5.Name = "Column5"
'
' Column6
'
Column6.HeaderText = "Availability"
Column6.Name = "Column6"
Column6.Resizable = DataGridViewTriState.True
Column6.SortMode = DataGridViewColumnSortMode.Automatic
'
' btnSave
'
btnSave.BackColor = Color.Lime
btnSave.Location = New Point(145, 323)
btnSave.Name = "btnSave"
btnSave.Size = New Size(75, 23)
btnSave.TabIndex = 12
btnSave.Text = "SAVE"
btnSave.UseVisualStyleBackColor = False
'
' btnUpdate
'
btnUpdate.BackColor = Color.FromArgb(CByte(255), CByte(128), CByte(0))
btnUpdate.Location = New Point(235, 323)
btnUpdate.Name = "btnUpdate"
btnUpdate.Size = New Size(75, 23)
btnUpdate.TabIndex = 13
btnUpdate.Text = "UPDATE"
btnUpdate.UseVisualStyleBackColor = False
'
' btnDelete
'
btnDelete.BackColor = Color.Red
btnDelete.Location = New Point(145, 352)
btnDelete.Name = "btnDelete"
btnDelete.Size = New Size(75, 23)
btnDelete.TabIndex = 14
btnDelete.Text = "DELETE"
btnDelete.UseVisualStyleBackColor = False
'
' btnClear
'
btnClear.BackColor = Color.DodgerBlue
btnClear.Location = New Point(235, 352)
btnClear.Name = "btnClear"
btnClear.Size = New Size(75, 23)
btnClear.TabIndex = 15
btnClear.Text = "CLEAR"
btnClear.UseVisualStyleBackColor = False
'
' tbSearch
'
tbSearch.Location = New Point(477, 372)
tbSearch.Name = "tbSearch"
tbSearch.Size = New Size(292, 23)
tbSearch.TabIndex = 17
'
' btnRefresh
'
btnRefresh.BackColor = SystemColors.AppWorkspace
btnRefresh.Location = New Point(173, 381)
btnRefresh.Name = "btnRefresh"
btnRefresh.Size = New Size(117, 23)
btnRefresh.TabIndex = 18
btnRefresh.Text = "REFRESH"
btnRefresh.UseVisualStyleBackColor = False
'
' btnSearch
'
btnSearch.BackColor = SystemColors.AppWorkspace
btnSearch.Location = New Point(354, 372)
btnSearch.Name = "btnSearch"
btnSearch.Size = New Size(117, 23)
btnSearch.TabIndex = 19
btnSearch.Text = "SEARCH"
btnSearch.UseVisualStyleBackColor = False
'
' Form1
'
AutoScaleDimensions = New SizeF(7F, 15F)
AutoScaleMode = AutoScaleMode.Font
BackColor = Color.Salmon
ClientSize = New Size(1168, 450)
Controls.Add(btnSearch)
Controls.Add(btnRefresh)
Controls.Add(tbSearch)
Controls.Add(btnClear)
Controls.Add(btnDelete)
Controls.Add(btnUpdate)
Controls.Add(btnSave)
Controls.Add(dgv)
Controls.Add(cbAvailability)
Controls.Add(dtpExDate)
Controls.Add(txtExpiry)
Controls.Add(cbProdGroup)
Controls.Add(txtProdGroup)
Controls.Add(tbPrice)
Controls.Add(txtPrice)
Controls.Add(tbName)
Controls.Add(txtProdName)
Controls.Add(tbProdNo)
Controls.Add(txtProdNo)
Name = "Form1"
Text = "VB_Quimbo"
CType(dgv, ComponentModel.ISupportInitialize).EndInit()
ResumeLayout(False)
PerformLayout()
End Sub

Friend WithEvents txtProdNo As Label


Friend WithEvents tbProdNo As TextBox
Friend WithEvents txtProdName As Label
Friend WithEvents tbName As TextBox
Friend WithEvents txtPrice As Label
Friend WithEvents tbPrice As TextBox
Friend WithEvents txtProdGroup As Label
Friend WithEvents cbProdGroup As ComboBox
Friend WithEvents txtExpiry As Label
Friend WithEvents dtpExDate As DateTimePicker
Friend WithEvents cbAvailability As CheckBox
Friend WithEvents dgv As DataGridView
Friend WithEvents Column1 As DataGridViewTextBoxColumn
Friend WithEvents Column2 As DataGridViewTextBoxColumn
Friend WithEvents Column3 As DataGridViewTextBoxColumn
Friend WithEvents Column4 As DataGridViewTextBoxColumn
Friend WithEvents Column5 As DataGridViewTextBoxColumn
Friend WithEvents Column6 As DataGridViewCheckBoxColumn
Friend WithEvents btnSave As Button
Friend WithEvents btnUpdate As Button
Friend WithEvents btnDelete As Button
Friend WithEvents btnClear As Button
Friend WithEvents tbSearch As TextBox
Friend WithEvents btnRefresh As Button
Friend WithEvents btnSearch As Button

End Class

You might also like