Save and Load Data in the Listview Using VB.Net and MySQL Database

Submitted by: 
Visitors have accessed this post 186 times.

If you find it hard to save and load the data in the Listview, this tutorial is just right for you. ListView is commonly used by many programmers to avoid logging in retrieving the data when the content is too much. By simply following the procedure below you will be able to perform it with ease.

Let's get started

Creating Database

Create a database named “test

After that, create a table in the database that you have created.

  1. CREATE TABLE `test`.`tblperson` ( `PersonID` INT NOT NULL AUTO_INCREMENT , `Fname`VARCHAR(90) NOT NULL , `Lname` VARCHAR(90) NOT NULL , `Mname` VARCHAR(90) NOT NULL, PRIMARY KEY (`PersonID`)) ENGINE = InnoDB;

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application. Then, do the form just like shown below.
lst

Step 2

Add MySQL.Data.dll as references.

Step 3

Double click the form and set the imports above the “Public Class”.

  1. Imports MySql.Data.MySqlClient

Step 4

Set a connection between MySQL Database and VB.Net. Then, 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 5

Do the following code inside the “form1_load” for retrieving data in the list view.

  1. Try
  2. con.Open()
  3. sql = "SELECT * FROM tblperson"
  4. cmd = New MySqlCommand
  5. With cmd
  6. .Connection = con
  7. .CommandText = sql
  8. dr = .ExecuteReader()
  9. End With
  10. ListView1.Items.Clear()
  11. Do While dr.Read = True
  12. Dim list = ListView1.Items.Add(dr(1))
  13. list.SubItems.Add(dr(2))
  14. list.SubItems.Add(dr(3))
  15. Loop
  16. Catch ex As Exception
  17. MsgBox(ex.Message)
  18. Finally
  19. con.Close()
  20. End Try

Step 6

Go back to the design view, double click the “Save” button to fire the click event handler and do the following code for saving the data in the database.

  1. Try
  2. con.Open()
  3. sql = "INSERT INTO tblperson (Fname,Mname,Lname) VALUES ('" & txtFname.Text & "','" & txtMname.Text & "','" & txtLname.Text & "')"
  4. cmd = New MySqlCommand
  5. With cmd
  6. .Connection = con
  7. .CommandText = sql
  8. result = .ExecuteNonQuery()
  9. End With
  10. If result = 1 Then
  11. MsgBox("Data has been saved in the database.")
  12. txtFname.Clear()
  13. txtLname.Clear()
  14. txtMname.Clear()
  15. Else
  16. MsgBox("error query!", MsgBoxStyle.Exclamation)
  17. End If
  18. Catch ex As Exception
  19. MsgBox(ex.Message)
  20. Finally
  21. con.Close()
  22. End Try
  23. Call Form1_Load(sender, e)

The complete sourcecode is included. You can download it and run it on your computer.

For more question about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
FB Account – https://www.facebook.com/onnaj.soicalap

Or feel free to comment below.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.