0% found this document useful (0 votes)
17 views4 pages

CRUDPICTUREBOX

Uploaded by

lawrencepogi122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

CRUDPICTUREBOX

Uploaded by

lawrencepogi122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Imports MySql.Data.

MySqlClient
Imports System.IO
Public Class Form1
Dim connection As New
MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=GIDDEY")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=")
Dim table As New DataTable()
Dim adapter As New MySqlDataAdapter("SELECT * FROM GIDDEY.JOSH", connection)

adapter.Fill(table)

DataGridView1.DataSource = table

End Sub
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles
DataGridView1.DataError
e.ThrowException = False
End Sub
Private Sub LoadData()
Dim table As New DataTable()
Dim adapter As New MySqlDataAdapter("SELECT * FROM GIDDEY.JOSH", connection)

adapter.Fill(table)

DataGridView1.DataSource = table

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)

Dim command As New MySqlCommand("Insert into


`JOSH`(`FULLNAME`,`EMAIL`,`DSCP`,`PICTURE`)VALUES(@nm,@eml,@ds,@img)", connection)
command.Parameters.Add("@nm", MySqlDbType.VarChar).Value = TextBox2.Text
command.Parameters.Add("@eml", MySqlDbType.VarChar).Value = TextBox3.Text
command.Parameters.Add("@ds", MySqlDbType.VarChar).Value = TextBox4.Text
command.Parameters.Add("@img", MySqlDbType.Blob).Value = ms.ToArray()

connection.Open()

If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("Image Inserted")

Else
MessageBox.Show("Image Not Inserted")
End If

connection.Close()
LoadData()

End Sub
Public Sub ExecuteQuery(query As String)
Dim command As New MySqlCommand(query, connection)

connection.Open()
command.ExecuteNonQuery()

connection.Close()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


Dim updateQuery As String = "UPDATE JOSH SET FULLNAME = @nm, EMAIL = @eml, DSCP = @ds,
PICTURE = @img WHERE ID = @ID"

Using command As New MySqlCommand(updateQuery, connection)

command.Parameters.AddWithValue("@nm", TextBox2.Text)
command.Parameters.AddWithValue("@eml", TextBox3.Text)
command.Parameters.AddWithValue("@ds", TextBox4.Text)

If PictureBox1.Image IsNot Nothing Then


Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
command.Parameters.Add("@img", MySqlDbType.Blob).Value = ms.ToArray()
Else

command.Parameters.AddWithValue("@img", DBNull.Value)
End If

command.Parameters.AddWithValue("@ID", TextBox1.Text)

connection.Open()
command.ExecuteNonQuery()
connection.Close()

End Using

MessageBox.Show("Data Updated")
LoadData()

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


Dim selectedRowIndex As Integer = -1

If DataGridView1.SelectedRows.Count > 0 Then


selectedRowIndex = DataGridView1.SelectedRows(0).Index
Else
MessageBox.Show("Please select a row to delete.")
Return
End If

Dim idToDelete As Integer = Convert.ToInt32(DataGridView1.Rows(selectedRowIndex).Cells("ID").Value)

Dim deleteQuery As String = "DELETE FROM JOSH WHERE ID = " & idToDelete
ExecuteQuery(deleteQuery)

If selectedRowIndex <> -1 Then


MessageBox.Show("User deleted")
End If

LoadData()

End Sub
Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)

End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles


DataGridView1.CellContentClick
TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value
TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value
TextBox3.Text = DataGridView1.CurrentRow.Cells(2).Value
TextBox4.Text = DataGridView1.CurrentRow.Cells(3).Value

Dim imageBytes As Byte() = TryCast(DataGridView1.CurrentRow.Cells(4).Value, Byte())

If imageBytes IsNot Nothing Then


Using ms As New MemoryStream(imageBytes)
PictureBox1.Image = Image.FromStream(ms)
End Using
Else
MessageBox.Show("Image data not found in the selected row.", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If

End Sub

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


Dim opf As New OpenFileDialog

opf.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"

If opf.ShowDialog = System.Windows.Forms.DialogResult.OK Then

PictureBox1.Image = Image.FromFile(opf.FileName)

End If
End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click


TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
PictureBox1.Image = Nothing
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Form2.Show()
Me.Hide()

End Sub
End Class

You might also like