0% found this document useful (0 votes)
13 views

Source Code VB

The document defines a class called Item_List that connects to a MySQL database and loads item data into a datagrid. It contains methods for loading, saving, updating, deleting and searching for items in the database tables. The class handles database connections, queries, and displaying results in the UI elements like text boxes and datagrid.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Source Code VB

The document defines a class called Item_List that connects to a MySQL database and loads item data into a datagrid. It contains methods for loading, saving, updating, deleting and searching for items in the database tables. The class handles database connections, queries, and displaying results in the UI elements like text boxes and datagrid.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Imports MySql.Data.

MySqlClient
Public Class Item_List
Dim conn As New
MySqlConnection("Datasource=localhost;port=3306;username=root;password=;database=ap
al_inventory_offline")
Dim i As Integer
Dim dr As MySqlDataReader
Private Sub Item_List_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
DVG_load()
End Sub

Public Sub DVG_load()


DataGridView1.Rows.Clear()
Try
conn.Open()
Dim cmd As New MySqlCommand("SELECT * FROM items", conn)
dr = cmd.ExecuteReader
While dr.Read
DataGridView1.Rows.Add(dr.Item("item_id"), dr.Item("name"),
dr.Item("price"), dr.Item("stock"), dr.Item("supplier"), dr.Item("notes"))
End While
dr.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
End Sub

Public Sub save()


Try
conn.Open()
Dim cmd As New MySqlCommand("INSERT INTO `items`(`item_id`, `name`,
`price`, `stock`, `supplier`, `notes`) VALUES
(@item_id,@name,@price,@stock,@supplier,@notes)", conn)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@item_id", itmId.Text)
cmd.Parameters.AddWithValue("@name", itmName.Text)
cmd.Parameters.AddWithValue("@price", CDec(itmPrice.Text))
cmd.Parameters.AddWithValue("@stock", itmStock.Text)
cmd.Parameters.AddWithValue("@supplier", itmSpplr.Text)
cmd.Parameters.AddWithValue("@notes", itmNotes.Text)

i = cmd.ExecuteNonQuery
If i > 0 Then
MessageBox.Show("Record Save Success!", "CURD",
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

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


btn_save.Click
save()
DVG_load()
End Sub

Public Sub clear()


itmId.Clear()
itmName.Clear()
itmPrice.Clear()
itmStock.Clear()
itmSpplr.Clear()
itmNotes.Clear()
End Sub

Private Sub DataGridView1_CellClick(sender As Object, e As


DataGridViewCellEventArgs) Handles DataGridView1.CellClick
itmId.Text = DataGridView1.CurrentRow.Cells(0).Value
itmName.Text = DataGridView1.CurrentRow.Cells(1).Value
itmPrice.Text = DataGridView1.CurrentRow.Cells(2).Value
itmStock.Text = DataGridView1.CurrentRow.Cells(3).Value
itmSpplr.Text = DataGridView1.CurrentRow.Cells(4).Value
itmNotes.Text = DataGridView1.CurrentRow.Cells(5).Value

itmId.ReadOnly = True
btn_save.Enabled = False

End Sub

Sub Edit()
Try
conn.Open()
Dim cmd As New MySqlCommand("UPDATE `items` SET `name`=@name,`price`=
@price,`stock`= @stock,`supplier`=@supplier,`notes`=@notes WHERE
`item_id`=@item_id", conn)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@item_id", itmId.Text)
cmd.Parameters.AddWithValue("@name", itmName.Text)
cmd.Parameters.AddWithValue("@price", CDec(itmPrice.Text))
cmd.Parameters.AddWithValue("@stock", itmStock.Text)
cmd.Parameters.AddWithValue("@supplier", itmSpplr.Text)
cmd.Parameters.AddWithValue("@notes", itmNotes.Text)

i = cmd.ExecuteNonQuery
If i > 0 Then
MessageBox.Show("Record Update Success!", "CURD",
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()
DVG_load()
End Sub

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


btn_update.Click
Edit()
End Sub

Public Sub delete()


If MsgBox("Are you sure to delete this record?", MsgBoxStyle.Question +
vbYesNo) = vbYes Then
Try
conn.Open()
Dim cmd As New MySqlCommand("DELETE FROM `items` WHERE
`item_id`=@item_id", conn)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@item_id", itmId.Text)

i = cmd.ExecuteNonQuery
If i > 0 Then
MessageBox.Show("Record Update Success!", "CURD",
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()
DVG_load()
End If
End Sub

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


btn_delete.Click
delete()
End Sub

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


btn_clear.Click
clear()
End Sub

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


search.TextChanged
DataGridView1.Rows.Clear()
Try
conn.Open()
Dim cmd As New MySqlCommand("SELECT * FROM items WHERE item_id like '%"
& search.Text & "%' OR name like '%" & search.Text & "%'", conn)
dr = cmd.ExecuteReader
While dr.Read
DataGridView1.Rows.Add(dr.Item("item_id"), dr.Item("name"),
dr.Item("price"), dr.Item("stock"), dr.Item("supplier"), dr.Item("notes"))
End While
dr.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
End Sub

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


Button4.Click
If MsgBox("Are you sure to logout?", MsgBoxStyle.Question + vbYesNo) =
vbYes Then
Me.Close()
Login.Show()
End If
End Sub
End Class

You might also like