07-Updating Employees

0
513

This tutorial is all about 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.
[vbnet] ‘A SUB PRUCEDURE FOR RETRIEVING DATA IN THE DATABASE
Public Sub mysqlRetrieveTextBox(ByVal query As String)
Try
‘OPENING THE CONNECTION
con.Open()
‘SET YOUR COMMANDS TO PROVIDE A TEXT-BASE INTERFACE INTO THE MYSQL DATABASE SERVER.
‘AND ONCE IT’S CONNECTED, YOU CAN MAKE QUERY OR MANY OTHER OPERATION.
With cmd
.Connection = con
.CommandText = query
End With
da = New MySqlDataAdapter
‘SET THIS STORED PROCEDURE TO SELECT THE RECORD IN THE DATASOURCE
da.SelectCommand = cmd
dt = New DataTable
da.Fill(dt)
‘CHECKING IF THE DATA IS ALREADY EXIST.
If dt.Rows.Count > 0 Then
With Form1
‘PUTTING THE DATA IN THE DATABASE TO A SPECIFIC FIELDS
.txtempId.Text = dt.Rows(0).Item(“EMPLOYEE_ID”)
.txtfname.Text = dt.Rows(0).Item(“FIRST_NAME”)
.txtlname.Text = dt.Rows(0).Item(“LAST_NAME”)
.txtmname.Text = dt.Rows(0).Item(“MIDDLE_NAME”)
.txtaddress.Text = dt.Rows(0).Item(“ADDRESS”)
.txtcontact.Text = dt.Rows(0).Item(“PHONE_NUMBER”)
.txtstatus.Text = dt.Rows(0).Item(“STATUS”)
.dtpdbirth.Value = dt.Rows(0).Item(“BIRTH_DATE”)
.txtbplace.Text = dt.Rows(0).Item(“BIRTH_PLACE”)
If dt.Rows(0).Item(“GENDER”) = “Male” Then
.rdomale.Checked = True
Else
.rdofemale.Checked = True
End If
.txtage.Text = dt.Rows(0).Item(“AGE”)
.txtemerg.Text = dt.Rows(0).Item(“EMERG_CONTACT”)
.txtdrate.Text = dt.Rows(0).Item(“D_RATE”)
.txtposition.Text = dt.Rows(0).Item(“POSITION”)
.txtpmethod.Text = dt.Rows(0).Item(“P_METHOD”)
.txtworkstatus.Text = dt.Rows(0).Item(“W_STATUS”)
.dtpdhired.Value = dt.Rows(0).Item(“D_HIRED”)
End With
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
‘CLOSING THE CONNECTION
con.Close()
da.Dispose()
End Sub
[/vbnet] 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.
[vbnet] Private Sub DataGridView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
Try
‘SET A QUERY FOR RETRIEVING DATA IN THE DATABASE
query = “SELECT * FROM employees as E, employeesworkinfo W ” & _
“WHERE E.EMPLOYEE_ID=W.EMPLOYEE_ID and E.EMPLOYEE_ID='” & _
DataGridView1.CurrentRow.Cells(0).Value & “‘”
‘CALL A METHOD THAT YOU HAVE CREATED
mysqlRetrieveTextBox(query)
‘SELECTING THE TABPAGE1
TabControl1.SelectTab(TabPage1)
‘DISABLE THE TEXTBOX ID
txtempId.Enabled = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
[/vbnet] 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.
[vbnet] Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
‘DECLARING A STRING VARIABLE
Dim gender As String
‘CHECKING IF THE RADIO BUTTON WAS CHECKED OR NOT
If rdomale.Checked = True Then
gender = “Male”
Else
gender = “Female”
End If
‘SET A QUERY FOR UPDATING RECORD IN THE EMPLOYEES TABLE.
query = “UPDATE employees E,employeesworkinfo W SET ” & _
FIRST_NAME='” & txtfname.Text & “‘, LAST_NAME='” & txtlname.Text & “‘, MIDDLE_NAME='” & txtmname.Text & _
“‘, ADDRESS='” & txtaddress.Text & “‘, PHONE_NUMBER='” & txtcontact.Text & “‘, STATUS='” & txtstatus.Text & _
“‘, BIRTH_DATE='” & Format(dtpdbirth.Value, “yyyy-MM-dd”) & “‘,BIRTH_PLACE='” & txtbplace.Text & _
“‘, GENDER='” & gender & “‘, AGE='” & txtage.Text & “‘, EMERG_CONTACT='” & txtemerg.Text & _
“‘, D_RATE='” & txtdrate.Text & “‘, P_METHOD= ‘” & txtpmethod.Text & “‘, POSITION = ‘” & txtposition.Text & _
“‘,W_STATUS='” & txtworkstatus.Text & “‘, D_HIRED='” & Format(dtpdhired.Value, “yyyy-MM-dd”) & “‘ ” & _
” WHERE E.EMPLOYEE_ID=W.EMPLOYEE_ID and E.EMPLOYEE_ID='” & txtempId.Text & “‘ ”
‘CALL THE METHOD THAT YOU HAVE CREATED
mysqlUpdate(query)
‘ENABLE THE TEXTBOX ID
txtempId.Enabled = False
End Sub
[/vbnet]

 

 

Readers might read also:

 

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.