13-Updating Users

1
419

This tutorial is all about 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.
[vbnet] Public Sub USERRETRIVETEXTBOX(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 ManageUserForm
.txtUserId.Text = dt.Rows(0).Item(“USER_ID”)
.txtName.Text = dt.Rows(0).Item(“UNAME”)
.txtUsername.Text = dt.Rows(0).Item(“USERNAME”)
.txtPassword.Text = dt.Rows(0).Item(“PASS”)
.cboType.Text = dt.Rows(0).Item(“TYPE”)
End With
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
‘CLOSING THE CONNECTION
con.Close()
da.Dispose()
End Sub
[/vbnet] 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.
[vbnet] Private Sub dtgList_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgList.DoubleClick, dtgList.RowHeaderMouseClick
query = “SELECT * FROM useraccounts WHERE USER_ID='” & dtgList.CurrentRow.Cells(0).Value & “‘”
USERRETRIVETEXTBOX(query)
End Sub
[/vbnet] 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.
[vbnet] Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
‘UPDATING DATA
query = “UPDATE useraccounts SET UNAME='” & txtName.Text & “‘, USERNAME='” & txtUsername.Text & _
“‘, PASS='” & txtPassword.Text & “‘, TYPE='” & cboType.Text & “‘ WHERE USER_ID='” & txtUserId.Text & “‘”
mysqlUpdate(query)

‘RETRIEVING DATA
‘STORE A SLECT QUERY IN A STRING VARIABLE
query = “SELECT USER_ID, UNAME AS NAME, USERNAME, TYPE FROM useraccounts
‘SET A RETRIEVE METHOD THAT YOU HAD CREATED.
mysqlRetrieve(query, dtgList)
End Sub
[/vbnet]

 

Readers might read also:

 

 

 

Facebook Comments

1 COMMENT

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.