05-Retrieve and Update Year Level

1
500

This tutorial is all about Retrieve and Update Year Level.
This time, i’m going to retrieve the records that have been saved in Ms Access Database. I have made some changes here regarding with the design of the Form.

 

Let’s begin:

 

 

Open the project that I have created last time which is “StudentInformation” and do the Form just like this.

retrieveupdateyl_form1

After setting up the Form, we’ re going to retrieve all the records in Ms Access Database. Now, double click the “Load” button to fire the event handler of it and do this following code to display all the records in the DatagridView.
[vbnet] Private Sub btnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click
Try
‘opening the connection
con.Open()
‘store a query to a variable that you have declared.
query = “SELECT * FROM tbllevel”
‘set a query and a connection string to a class that you have declared.
da = New OleDb.OleDbDataAdapter(query, con)
dt = New DataTable
‘Filling the data in the datatable
da.Fill(dt)
‘Get or set the datasource of a datagridview to display the data that came from the database.
dtgyllist.DataSource = dt

Catch ex As Exception
‘catching error
MsgBox(ex.Message)
End Try
con.Close()
End Sub
[/vbnet] Go back to the Form Design and click the DatagridView. After that, go to the properties and click the lightning symbol. Scroll down and find the “DoubleClick” event handler of the DatagridView and double click it.
retrieveupdateyl_form2
In the DoubleClick event handler of the DatagridView, add this following code for passing the record from the DatagridView to a TextBox.
[vbnet] Private Sub dtgyllist_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgyllist.DoubleClick
Try
‘passing the value to a variable
levelid = dtgyllist.CurrentRow.Cells(0).Value
‘passing the value to a textbox
txtyl.Text = dtgyllist.CurrentRow.Cells(1).Value
Catch ex As Exception
‘catching error
MsgBox(ex.Message)
End Try
End Sub
[/vbnet] Lastly, go back to the Form Design and double click the “Update” Button to fire the event handler of it. After that, add this following code in the method.
[vbnet] Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
Try
‘opening the connection
con.Open()
‘store a query to a variable that you have declared.
query = “UPDATE tbllevel SET YLEVEL = ‘” & txtyl.Text & “‘ WHERE LEVELID =” & levelid & “”
‘set a query and a connection string to a class that you have declared.
da = New OleDb.OleDbDataAdapter(query, con)
dt = New DataTable
‘Filling the data in the datatable
da.Fill(dt)

MsgBox(“Year Level has been updated.”)
txtyl.Clear()
levelid = 0
Catch ex As Exception
‘catching error
MsgBox(ex.Message)
End Try
con.Close()
End Sub
[/vbnet]

 

 

Readers might read also:

08-Retrieve and Update Academic Year

 

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.