Update and Load Data in the ListView Using VB.Net

In this tutorial, I will teach you how to Update and Load Data in the ListView Using VB.Net. With this method, it will help you retrieve the data in the ListView and at the same time you can update the data. See the procedure below.

Creating Database

Create a database named “test”. After that, add a table in the database that you have created.
  1. CREATE TABLE `tblperson` (
  2.   `PersonID` int(11) NOT NULL,
  3.   `Fname` varchar(90) NOT NULL,
  4.   `Lname` varchar(90) NOT NULL,
  5.   `Mname` varchar(90) NOT NULL
Then, insert the data in the table.
  1. INSERT INTO `tblperson` (`PersonID`, `Fname`, `Lname`, `Mname`) VALUES
  2. (1, 'Janno', 'Palacios', 'E'),
  3. (2, 'John Craig', 'Nillos', 'P');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application. Then, do the form just like this. lst2

Step 2

Set the imports.
  1. Imports MySql.Data.MySqlClient

Step 3

Create a connection between MySQL Database and VB.Net. After that, declare all the classes that are needed.
  1. Dim con As New MySqlConnection("server=localhost;user id=root;Password=janobe;database=test;sslMode=none")
  2.     Dim cmd As MySqlCommand
  3.     Dim dr As MySqlDataReader
  4.     Dim sql As String
  5.     Dim result As Integer

Step 4

Add the following code for loading data inside the ListView in the first load of the form.
  1.  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.         Try
  3.             con.Open()
  4.             sql = "SELECT * FROM tblperson"
  5.             cmd = New MySqlCommand
  6.             With cmd
  7.                 .Connection = con
  8.                 .CommandText = sql
  9.                 dr = .ExecuteReader()
  10.             End With
  11.             ListView1.Items.Clear()
  12.             Do While dr.Read = True
  13.                 Dim list = ListView1.Items.Add(dr(0))
  14.                 list.SubItems.Add(dr(1))
  15.                 list.SubItems.Add(dr(2))
  16.                 list.SubItems.Add(dr(3))
  17.             Loop
  18.         Catch ex As Exception
  19.             MsgBox(ex.Message)
  20.         Finally
  21.             con.Close()
  22.         End Try
  23.     End Sub

Step 5

Do the following code for updating the data in the MySQL database.
  1.     Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
  2.         Try
  3.             con.Open()
  4.             sql = "UPDATE tblperson SET Fname='" & txtFname.Text & "', Mname='" & txtMname.Text & "',Lname='" & txtLname.Text & "' WHERE PersonID=" & ListView1.FocusedItem.SubItems(0).Text
  5.             cmd = New MySqlCommand
  6.             With cmd
  7.                 .Connection = con
  8.                 .CommandText = sql
  9.                 result = .ExecuteNonQuery()
  10.             End With
  11.             If result = 1 Then
  12.                 MsgBox("Data has been updated in the database.")
  13.                 txtFname.Clear()
  14.                 txtLname.Clear()
  15.                 txtMname.Clear()
  16.             Else
  17.                 MsgBox("error query!", MsgBoxStyle.Exclamation)
  18.             End If
  19.         Catch ex As Exception
  20.             MsgBox(ex.Message)
  21.         Finally
  22.             con.Close()
  23.         End Try
  24.         Call Form1_Load(sender, e)
  25.     End Sub

Step 6

Do the following code for passing the data from the listview to the textbox.
  1. Private Sub ListView1_Click(sender As Object, e As EventArgs) Handles ListView1.Click
  2.         Try
  3.             txtFname.Text = ListView1.FocusedItem.SubItems(1).Text
  4.             txtMname.Text = ListView1.FocusedItem.SubItems(2).Text
  5.             txtLname.Text = ListView1.FocusedItem.SubItems(3).Text
  6.         Catch ex As Exception
  7.             MsgBox(ex.Message)
  8.  
  9.         End Try
  10.     End Sub
Note: add MySQL.Data.dll as references to access MySQL Library.

Add new comment