Updating Data in VB.Net and SQL Server 2018

This is the continuation of my previous tutorial which is Retrieving Data in VB.Net and SQL Server 2018. This time I will teach you how to update the data in VB.Net and SQL Server 2018. In here, you will be able to update the data from the database. This method is so simple and easy to follow if you are a newbie in programming. Let's begin.

Creating Application

Step 1

Open Retrieving Data in VB.Net and SQL Server 2018.

Step 2

Add a button in the Form and design the Form just like shown below. figure 1

Step 3

Press F7 to open the code editor. In the code editor, add a method for retrieving single result data in the database.
  1.        
  2.     Private Sub retrieveSingleResult(sql As String)
  3.         Dim da As New SqlDataAdapter
  4.         Dim dt As New DataTable
  5.  
  6.         Try
  7.             con.Open()
  8.             cmd = New SqlCommand
  9.  
  10.             With cmd
  11.                 .Connection = con
  12.                 .CommandText = sql
  13.             End With
  14.             da.SelectCommand = cmd
  15.             da.Fill(dt)
  16.  
  17.             txtFname.Text = dt.Rows(0).Item(1)
  18.             txtLname.Text = dt.Rows(0).Item(2)
  19.  
  20.  
  21.         Catch ex As Exception
  22.             MsgBox(ex.Message)
  23.         Finally
  24.             con.Close()
  25.             da.Dispose()
  26.         End Try
  27.     End Sub

Step 4

Write the following code for passing the data from the datagridview to the textboxes when the current cell is clicked.
  1.        
  2.     Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  3.         sql = "SELECT * FROM tblperson WHERE PersonID=" & DataGridView1.CurrentRow.Cells(0).Value
  4.         retrieveSingleResult(sql)
  5.     End Sub

Step 5

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

Step 6

Go back to the design view, double click the “Update” button to open the click event handler of it. After that, add this code inside the “button3_clicked” event to update the data in the database.
  1.      
  2.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  3.         sql = "UPDATE tblperson SET Fname='" & txtFname.Text & "',Lname='" & txtLname.Text & "' WHERE PersonID=" & DataGridView1.CurrentRow.Cells(0).Value
  4.         saveData(sql)
  5.  
  6.         txtFname.Clear()
  7.         txtLname.Clear()
  8.     End Sub

Previous Tutorial

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