Part III: Update and Delete Specific Data in MySQL Database using Visual Basic.Net

In this lesson we will focus on completing the user CRUD tutorial or the Create, Read, Update and Delete user. So to start with, Open our last tutorial called "Part II: Inserting and Reading of Data in MySQL Database Using Visual Basic.Net" and select the Manage User form and add two buttons in it. The first button we will name it as “btnupdate” and the text of this button we be “Update User”, and the other button we name it as “btnDelete” as the text will be “Delete User”. The form now will look same as shown below. Next we’re going to add functionality in Update Button. But first we need to make sure that, what we’re going to update is the specific data that has been selected by the user. Meaning the process to do this action is that the first will load all the data into the datagridview that has been saved to the database by clicking the “Read User”. And when the user clicks the specific data in the datagridview, it will show in the textbox that correspond to its value. And one of the most important one, is that we need to add another label that will hold later the value of Id Number of specific data that has been selected by the user. The code will be based on the “datagridview cell Mouse Click”.
  1. Private Sub dtguser_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dtguser.CellMouseClick
  2.         'this code will simply pass the value from the specific row selected by the user
  3.         lblid.Text = dtguser.CurrentRow.Cells(0).Value
  4.         txtfullname.Text = dtguser.CurrentRow.Cells(1).Value
  5.         txtusername.Text = dtguser.CurrentRow.Cells(2).Value
  6.         txtpassword.Text = dtguser.CurrentRow.Cells(3).Value
  7.  
  8.     End Sub
To do this just simply add another label and Name it as “lblid” and make it Invisible to the user by setting the “visible” property into “false”. So that when we run the program this will not be visible to the user. I think this time we are ready to add functionality to our “btnupdate” button. To do this just double click the “ btnupdate” button and add this code.
  1. 'this line of is simply we copy this one from our form
  2.         con.ConnectionString = ("server=localhost;user id=root;password=;database=test")
  3.  
  4.         Try
  5.             'we open Connection
  6.             con.Open()
  7.  
  8.             With cmd
  9.                 .Connection = con
  10.                 .CommandText = "UPDATE  `test`.`users` SET  `fullname` =  '" & txtfullname.Text & "',`username` =  '" & txtusername.Text & "',`password` =  '" & txtpassword.Text & "' WHERE  `users`.`user_id` =" & Val(lblid.Text) & ";"
  11.                 'in this line it Executes a transact-SQL statements against the connection and returns the number of rows affected
  12.                 result = cmd.ExecuteNonQuery
  13.                 'if the result is equal to zero it means that no rows is inserted or somethings wrong during the execution
  14.                 If result = 0 Then
  15.                     MsgBox("Data has been Updated!")
  16.                 Else
  17.                     MsgBox("Successfully Updated!")
  18.  
  19.                 End If
  20.             End With
  21.         Catch ex As Exception
  22.             MsgBox(ex.Message)
  23.         End Try
  24.         con.Close()
  25.         Call btnload_Click(sender, e)
  26.  
  27.     End Sub
And after this we can now test if this code works same as we expected. By running the program and clicking the “Read User” button. And clicking the specific data in the datagridview and changing the data based on what we wanted. For the “btnDelete” we can just simply copy the code in the “btnupdate” and only the difference is the query, so we can write it like this.
  1. "DELETE FROM `test`.`users` WHERE `users`.`user_id` =" & Val(lblid.Text) & ";"

Comments

Hello,
thank you for such a great tutorial. I am new to VB.Net database and this has help me a lot for a better understanding. A small request could include a search button to this interface! Thanks in advance

thank for this informative tutorial, I've learned more about sql commands. maraming salamat po kuya(thank you very much brother).. hehe

im getting error on delete and update, it always loop If result = 0 Then MsgBox("Data has been Updated!") Else MsgBox("Successfully Updated!") End If

Add new comment