ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

13-Updating Users

In this tutorial, I will teach you how to update the records in the MySQL Database. With, this you can update the user’s information in the database and I’m going to use the same process just like what i did on 07-Updating Employees.

 

 

Let’s Begin:

 

 

Open the file of “EmployeesInformationSystem” that you had created. after that, Add a Button and name it “btnupdate“. This Button is used for updating the user’s records. Now, set the Form just like this.
userregformupdateform
After setting up the Form, go to the module(“dbconnection”) and create a method for retrieving a specific data in a corresponding objects.

  1. Public Sub USERRETRIVETEXTBOX(ByVal query As String)
  2. Try
  3. 'OPENING THE CONNECTION
  4. con.Open()
  5. 'SET YOUR COMMANDS TO PROVIDE A TEXT-BASE INTERFACE INTO THE MYSQL DATABASE SERVER.
  6. 'AND ONCE IT'S CONNECTED, YOU CAN MAKE QUERY OR MANY OTHER OPERATION.
  7. With cmd
  8. .Connection = con
  9. .CommandText = query
  10. End With
  11. da = New MySqlDataAdapter
  12. 'SET THIS STORED PROCEDURE TO SELECT THE RECORD IN THE DATASOURCE
  13. da.SelectCommand = cmd
  14. dt = New DataTable
  15. da.Fill(dt)
  16. 'CHECKING IF THE DATA IS ALREADY EXIST.
  17. If dt.Rows.Count > 0 Then
  18. With ManageUserForm
  19. .txtUserId.Text = dt.Rows(0).Item("USER_ID")
  20. .txtName.Text = dt.Rows(0).Item("UNAME")
  21. .txtUsername.Text = dt.Rows(0).Item("USERNAME")
  22. .txtPassword.Text = dt.Rows(0).Item("PASS")
  23. .cboType.Text = dt.Rows(0).Item("TYPE")
  24. End With
  25. End If
  26. Catch ex As Exception
  27. MsgBox(ex.Message)
  28. End Try
  29. 'CLOSING THE CONNECTION
  30. con.Close()
  31. da.Dispose()
  32. End Sub

Go back to “ManageUserForm” Form Design and click a DataGridView. After that, go to the properties and hit the events just like a lightning. In the events, find a “DoubleClick” event handler and double click it.
userregformupdateevent
In the dtgList_DoubleClick, add the following code for retrieving the data in the database to put it into the TextBox. The function of this is, when you double click a specific row in the DataGridView, the record in it will automatically appear in a corresponding TextBox.

  1. Private Sub dtgList_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgList.DoubleClick, dtgList.RowHeaderMouseClick
  2. query = "SELECT * FROM `useraccounts` WHERE `USER_ID`='" & dtgList.CurrentRow.Cells(0).Value & "'"
  3. USERRETRIVETEXTBOX(query)
  4. End Sub

Now, go back to the “ManageUserForm” Form Design and double click the “Update” Button to fire a Click event handler of it. After that, add the following code for updating and retrieving the users records in the database. The function of this, is when the”Update” Button is click, It well automatically update and refresh the list of records in the DataGridView.

  1. Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
  2. 'UPDATING DATA
  3. query = "UPDATE `useraccounts` SET `UNAME`='" & txtName.Text & "', `USERNAME`='" & txtUsername.Text & _
  4. "', `PASS`='" & txtPassword.Text & "', `TYPE`='" & cboType.Text & "' WHERE USER_ID='" & txtUserId.Text & "'"
  5. mysqlUpdate(query)
  6.  
  7. 'RETRIEVING DATA
  8. 'STORE A SLECT QUERY IN A STRING VARIABLE
  9. query = "SELECT `USER_ID`, `UNAME` AS NAME, `USERNAME`, `TYPE` FROM `useraccounts` "
  10. 'SET A RETRIEVE METHOD THAT YOU HAD CREATED.
  11. mysqlRetrieve(query, dtgList)
  12. End Sub
Facebook Comments

Check Your Domain Ranking

1 Comment

Add a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!