How to Save and Load Data Automatically in VB.Net and SQL Server 2018

In this tutorial, I will teach you How to Save and Load Data Automatically in VB.Net and SQL Server 2018. This method has the ability to save the data in the database and it will automatically display in the DataGridView. In this way, you can lessen the process of the current system you are working on now. This process is usually used in all data entry in the system. This program is made of Microsoft Visual Studio 2015 and SQL Server Database 2018. Let’s Begin:

Creating Database

1. Install the MSSMS 2018 on your machine. 2. Open the MSSMS 2018 . After that, right click the database, then select “New Database” and name it “dbpersonfigure 3 3. Do the following query to create a table in the database that you have created
  1.  
  2. USE [dbperson]
  3. GO
  4.  
  5. /****** Object:  Table [dbo].[tblperson]    Script Date: 11/10/2019 11:15:11 AM ******/
  6. SET ANSI_NULLS ON
  7. GO
  8.  
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11.  
  12. CREATE TABLE [dbo].[tblperson](
  13.         [PersonID] [INT] IDENTITY(1,1) NOT NULL,
  14.         [Fname] [nvarchar](50) NULL,
  15.         [Lname] [nvarchar](50) NULL
  16. ) ON [PRIMARY]
  17. GO

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for visual basic. ps1

Step 2

Add two Textboxes, a Button, and a DataGridview inside the Form. Then do the Form just like shown below. figure 1

Step 3

Press F7 to open the code editor. In the code editor, add a namespace to access SQL Server libraries.
  1.      
  2. Imports System.Data.SqlClient

Step 4

Create a connection between Visual Basic 2015 and SQL Server database. After that, declare and initialize all the classes and variables that are needed.
  1.      
  2.     Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;Database=dbperson;trusted_connection=true;")
  3.     Dim cmd As SqlCommand
  4.     Dim da As SqlDataAdapter
  5.     Dim dt As DataTable
  6.     Dim sql As String
  7.     Dim result As Integer

Step 5

Create a method for saving the data in the database.
  1.      
  2.     Private Sub saveData(sql As String)
  3.         Try
  4.             con.Open()
  5.             cmd = New SqlCommand()
  6.  
  7.             With cmd
  8.                 .Connection = con
  9.                 .CommandText = sql
  10.                 result = .ExecuteNonQuery()
  11.             End With
  12.  
  13.             If result > 0 Then
  14.                 MsgBox("Data has been saved in the database!")
  15.             Else
  16.                 MsgBox("Error to execute the query!")
  17.             End If
  18.         Catch ex As Exception
  19.             MsgBox(ex.Message)
  20.         Finally
  21.             con.Close()
  22.         End Try
  23.     End Sub

Step 6

Create a method for retrieving the data in the database.
  1.      
  2.     Private Sub loadData(sql As String, dtg As DataGridView)
  3.         Try
  4.             con.Open()
  5.             cmd = New SqlCommand()
  6.             da = New SqlDataAdapter
  7.             dt = New DataTable
  8.  
  9.             With cmd
  10.                 .Connection = con
  11.                 .CommandText = sql
  12.             End With
  13.             With da
  14.                 .SelectCommand = cmd
  15.                 .Fill(dt)
  16.             End With
  17.             dtg.DataSource = dt
  18.  
  19.         Catch ex As Exception
  20.             MsgBox(ex.Message)
  21.         Finally
  22.             con.Close()
  23.             da.Dispose()
  24.         End Try
  25.     End Sub

Step 7

Go back to the design view, double click the “Save” button to open the click event handler on it. After that, add this code inside the “Button1_Click” event to save the data in the database.
  1.        
  2.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  3.         sql = "INSERT INTO tblperson (Fname,Lname) values ('" & txtfname.Text & "','" & txtlname.Text & "')"
  4.         saveData(sql)
  5.  
  6.  
  7.         sql = "SELECT Fname as Firstname, Lname as Lastname FROM tblperson"
  8.         loadData(sql, dtgList)
  9.  
  10.     End Subs

Step 7

Write the following codes for retrieving data in the database in the first load of the form.
  1.  
  2.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3.         sql = "SELECT Fname as Firstname, Lname as Lastname FROM tblperson"
  4.         loadData(sql, dtgList)
  5.     End Sub
The complete source code is included. You can download it and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment