Retrieve Image from Database using DataGridView Click Event in VB.Net

0
155

Retrieve Image from Database using DataGridView Click Event in VB.Net

Retrieve Image from Database using DataGridView Click Event in VB.Net.

In this tutorial, you will learn how to retrieve images from your database by just pointing your mouse at a data grid view row.

Let’s assume that you have your own database with an image and its value in it. If you want to review about storing and retrieving images please take a look at this tutorial, Add and Retrieve Images in VB.Net.

Create a new project and name your project as you like.

Next, drag a datagridview and a pictureBox. Make sure to set your pictureBox size mode property to StretchImage. Here’s mine:

Retrieve Image from Database using DataGridView Click Event

Sample Database Columns

Retrieve Image from Database using DataGridView Click Event

Sample DataGridView and PictureBox.

Let’s first add a line of code where we will show only the ID values in the datagridview. Add this line of code to your Form1_Load or by just simply double clicking your Form1 designer.

Importante Note: You should import Mysql.Data.dll to your project for you to access the mySQL keywords and methods.

  1. Imports MySql.Data.MySqlClient
  2. Public Class Form1
  3.  
  4. Dim conn As MySqlConnection
  5. Dim cmd As MySqlCommand
  6. Dim da As MySqlDataAdapter
  7. Dim dt As DataTable
  8. Dim sql As String
  9. Dim rowIndex As Integer
  10. Dim row As DataGridViewRow
  11. Dim id As String
  12. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  13. Try
  14. conn = New MySqlConnection
  15. conn.ConnectionString = "server=localhost; userid=root; password=; database=retrieveimage;"
  16. conn.Open()
  17. sql = "SELECT ID FROM image;"
  18. cmd = New MySqlCommand(sql, conn)
  19. da = New MySqlDataAdapter
  20. dt = New DataTable
  21. da.SelectCommand = cmd
  22. da.Fill(dt)
  23. conn.Close()
  24. DataGridView1.DataSource = dt
  25. Catch ex As MySqlException
  26. MsgBox(ex.Message)
  27. Finally
  28. conn.Close()
  29. da.Dispose()
  30. End Try
  31. End Sub
  32. End Class

Next, we are going to add a line of code to our datagridview cellclick event:

  1. Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  2. If e.RowIndex >= 0 Then
  3. rowIndex = e.RowIndex
  4. row = DataGridView1.Rows(rowIndex)
  5. Try
  6. id = row.Cells(0).Value.ToString()
  7. conn = New MySqlConnection
  8. conn.ConnectionString = "server=localhost; userid=root; password=; database=retrieveimage;"
  9. conn.Open()
  10. sql = "SELECT * FROM image WHERE ID = @ID;"
  11. cmd = New MySqlCommand
  12. With cmd
  13. .Connection = conn
  14. .CommandText = sql
  15. .Parameters.Clear()
  16. .Parameters.AddWithValue("@ID", id.ToString)
  17. .ExecuteNonQuery()
  18. End With
  19. dt = New DataTable
  20. da = New MySqlDataAdapter
  21. da.SelectCommand = cmd
  22. da.Fill(dt)
  23. If dt.Rows.Count > 0 Then
  24. arrImage = dt.Rows(0).Item(1)
  25. Dim mstream As New System.IO.MemoryStream(arrImage)
  26. PictureBox1.Image = Image.FromStream(mstream)
  27. End If
  28. Catch ex As MySqlException
  29. MsgBox(ex.Message)
  30. Finally
  31. conn.Close()
  32. da.Dispose()
  33. End Try
  34. End If
  35. End Sub

After adding this line of code to your cellclick event. Run your system and try to select a value on your datagridview row. Here’s the result:

Congratulations! You have recently learned how to Retrieve Image from Database using DataGridView Click Event.

For questions or any other concerns or thesis/capstone creation with documentation, you can contact me through the following:

E-Mail: [email protected]

Facebook: facebook.com/kirk.lavapiez

Contact No.: +639771069640

To download the sample project, click here.

Ian Hero L. Lavapiez

BSIT Graduate

System Analyst and Developer

Related topic(s) that you may like:

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here