05-Retrieve and Update Year Level

1
424

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.

  1. Private Sub btnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click
  2. Try
  3. 'opening the connection
  4. con.Open()
  5. 'store a query to a variable that you have declared.
  6. query = "SELECT * FROM tbllevel"
  7. 'set a query and a connection string to a class that you have declared.
  8. da = New OleDb.OleDbDataAdapter(query, con)
  9. dt = New DataTable
  10. 'Filling the data in the datatable
  11. da.Fill(dt)
  12. 'Get or set the datasource of a datagridview to display the data that came from the database.
  13. dtgyllist.DataSource = dt
  14.  
  15. Catch ex As Exception
  16. 'catching error
  17. MsgBox(ex.Message)
  18. End Try
  19. con.Close()
  20. End Sub

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.

  1. Private Sub dtgyllist_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgyllist.DoubleClick
  2. Try
  3. 'passing the value to a variable
  4. levelid = dtgyllist.CurrentRow.Cells(0).Value
  5. 'passing the value to a textbox
  6. txtyl.Text = dtgyllist.CurrentRow.Cells(1).Value
  7. Catch ex As Exception
  8. 'catching error
  9. MsgBox(ex.Message)
  10. End Try
  11. End Sub

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.

  1. Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
  2. Try
  3. 'opening the connection
  4. con.Open()
  5. 'store a query to a variable that you have declared.
  6. query = "UPDATE tbllevel SET YLEVEL = '" & txtyl.Text & "' WHERE LEVELID =" & levelid & ""
  7. 'set a query and a connection string to a class that you have declared.
  8. da = New OleDb.OleDbDataAdapter(query, con)
  9. dt = New DataTable
  10. 'Filling the data in the datatable
  11. da.Fill(dt)
  12.  
  13. MsgBox("Year Level has been updated.")
  14. txtyl.Clear()
  15. levelid = 0
  16. Catch ex As Exception
  17. 'catching error
  18. MsgBox(ex.Message)
  19. End Try
  20. con.Close()
  21. End Sub

 

 

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