ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

07-Updating Employees

In this tutorial, I will teach you how to update the records of the Employee’s Information. With this, you can update the records of the Employee and I’m going to add a method for retrieving the specific record in the specific object.

Let’s begin:

Open the file “EmployeesInformationSystem” that you had created. After that, drag a Button beside the “Save” Button in the “Add New Employee” TabPage and it will look like this.
employeesregformupdatedataform
After adding a Button, go to the module and create a method for retrieving the data in the database to put it into the object.

  1. 'A SUB PRUCEDURE FOR RETRIEVING DATA IN THE DATABASE
  2. Public Sub mysqlRetrieveTextBox(ByVal query As String)
  3. Try
  4. 'OPENING THE CONNECTION
  5. con.Open()
  6. 'SET YOUR COMMANDS TO PROVIDE A TEXT-BASE INTERFACE INTO THE MYSQL DATABASE SERVER.
  7. 'AND ONCE IT'S CONNECTED, YOU CAN MAKE QUERY OR MANY OTHER OPERATION.
  8. With cmd
  9. .Connection = con
  10. .CommandText = query
  11. End With
  12. da = New MySqlDataAdapter
  13. 'SET THIS STORED PROCEDURE TO SELECT THE RECORD IN THE DATASOURCE
  14. da.SelectCommand = cmd
  15. dt = New DataTable
  16. da.Fill(dt)
  17. 'CHECKING IF THE DATA IS ALREADY EXIST.
  18. If dt.Rows.Count > 0 Then
  19. With Form1
  20. 'PUTTING THE DATA IN THE DATABASE TO A SPECIFIC FIELDS
  21. .txtempId.Text = dt.Rows(0).Item("EMPLOYEE_ID")
  22. .txtfname.Text = dt.Rows(0).Item("FIRST_NAME")
  23. .txtlname.Text = dt.Rows(0).Item("LAST_NAME")
  24. .txtmname.Text = dt.Rows(0).Item("MIDDLE_NAME")
  25. .txtaddress.Text = dt.Rows(0).Item("ADDRESS")
  26. .txtcontact.Text = dt.Rows(0).Item("PHONE_NUMBER")
  27. .txtstatus.Text = dt.Rows(0).Item("STATUS")
  28. .dtpdbirth.Value = dt.Rows(0).Item("BIRTH_DATE")
  29. .txtbplace.Text = dt.Rows(0).Item("BIRTH_PLACE")
  30. If dt.Rows(0).Item("GENDER") = "Male" Then
  31. .rdomale.Checked = True
  32. Else
  33. .rdofemale.Checked = True
  34. End If
  35. .txtage.Text = dt.Rows(0).Item("AGE")
  36. .txtemerg.Text = dt.Rows(0).Item("EMERG_CONTACT")
  37. .txtdrate.Text = dt.Rows(0).Item("D_RATE")
  38. .txtposition.Text = dt.Rows(0).Item("POSITION")
  39. .txtpmethod.Text = dt.Rows(0).Item("P_METHOD")
  40. .txtworkstatus.Text = dt.Rows(0).Item("W_STATUS")
  41. .dtpdhired.Value = dt.Rows(0).Item("D_HIRED")
  42. End With
  43. End If
  44. Catch ex As Exception
  45. MsgBox(ex.Message)
  46. End Try
  47. 'CLOSING THE CONNECTION
  48. con.Close()
  49. da.Dispose()
  50. End Sub

After creating a method, go to the Form, then click the “List Of Employees” TabPage and click again the DataGridView. After that, go to the properties and click the events that looks like a lightning. In the events, double click the “DoubleClick” to fire the DoubleClick event handler of the DataGridView.
employeesregformupdatedataevents
In the DataGridView1_DoubleClick, do this following code for calling the method that you have created to make it work.

  1. Private Sub DataGridView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
  2. Try
  3. 'SET A QUERY FOR RETRIEVING DATA IN THE DATABASE
  4. query = "SELECT * FROM `employees` as E, `employeesworkinfo` W " & _
  5. "WHERE E.`EMPLOYEE_ID`=W.`EMPLOYEE_ID` and E.`EMPLOYEE_ID`='" & _
  6. DataGridView1.CurrentRow.Cells(0).Value & "'"
  7. 'CALL A METHOD THAT YOU HAVE CREATED
  8. mysqlRetrieveTextBox(query)
  9. 'SELECTING THE TABPAGE1
  10. TabControl1.SelectTab(TabPage1)
  11. 'DISABLE THE TEXTBOX ID
  12. txtempId.Enabled = True
  13. Catch ex As Exception
  14. MsgBox(ex.Message)
  15. End Try
  16. End Sub

After that, double click the “Update” Button to fire the Click event handler of it and add this code in the method for updating the Employee’s records.

  1. Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
  2. 'DECLARING A STRING VARIABLE
  3. Dim gender As String
  4. 'CHECKING IF THE RADIO BUTTON WAS CHECKED OR NOT
  5. If rdomale.Checked = True Then
  6. gender = "Male"
  7. Else
  8. gender = "Female"
  9. End If
  10. 'SET A QUERY FOR UPDATING RECORD IN THE EMPLOYEES TABLE.
  11. query = "UPDATE `employees` E,`employeesworkinfo` W SET " & _
  12. "`FIRST_NAME`='" & txtfname.Text & "', `LAST_NAME`='" & txtlname.Text & "', `MIDDLE_NAME`='" & txtmname.Text & _
  13. "', `ADDRESS`='" & txtaddress.Text & "', `PHONE_NUMBER`='" & txtcontact.Text & "', `STATUS`='" & txtstatus.Text & _
  14. "', `BIRTH_DATE`='" & Format(dtpdbirth.Value, "yyyy-MM-dd") & "',`BIRTH_PLACE`='" & txtbplace.Text & _
  15. "', `GENDER`='" & gender & "', `AGE`='" & txtage.Text & "', `EMERG_CONTACT`='" & txtemerg.Text & _
  16. "', `D_RATE`='" & txtdrate.Text & "', `P_METHOD`= '" & txtpmethod.Text & "', POSITION = '" & txtposition.Text & _
  17. "',`W_STATUS`='" & txtworkstatus.Text & "', `D_HIRED`='" & Format(dtpdhired.Value, "yyyy-MM-dd") & "' " & _
  18. " WHERE E.`EMPLOYEE_ID`=W.`EMPLOYEE_ID` and E.`EMPLOYEE_ID`='" & txtempId.Text & "' "
  19. 'CALL THE METHOD THAT YOU HAVE CREATED
  20. mysqlUpdate(query)
  21. 'ENABLE THE TEXTBOX ID
  22. txtempId.Enabled = False
  23. End Sub
Facebook Comments

Check Your Domain Ranking

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!